Added logger singleton

This commit is contained in:
stratic-dev
2024-10-11 00:15:11 +01:00
parent 32dbfa7514
commit 44692e986e
12 changed files with 165 additions and 74 deletions

27
utils/logger/logger.go Normal file
View 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
}