mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-16 17:05:03 +01:00
Added logger singleton
This commit is contained in:
27
utils/logger/logger.go
Normal file
27
utils/logger/logger.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
instance Logger // Global instance of Logger interface
|
||||
once sync.Once // Ensures logger is initialized only once
|
||||
)
|
||||
|
||||
// Init initializes the global logger. This function should be called once, ideally during the app startup.
|
||||
func Init(zapLogger *zap.Logger) {
|
||||
once.Do(func() {
|
||||
instance = NewZapLogger(zapLogger) // Assign the zapLogger as the global logger
|
||||
})
|
||||
}
|
||||
|
||||
// Get returns the global logger instance.
|
||||
func Get() Logger {
|
||||
if instance == nil {
|
||||
panic("Logger is not initialized. Call logger.Init() before using the logger.")
|
||||
}
|
||||
return instance
|
||||
}
|
||||
Reference in New Issue
Block a user