refactor: logger does not need to be a singleton

This commit is contained in:
Derrick Hammer 2024-01-15 19:43:00 -05:00
parent eae3de419c
commit 8ceb8c1eb9
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 12 additions and 18 deletions

View File

@ -123,7 +123,7 @@ func (p *PortalImpl) getInitFuncs() []func() error {
}, },
func() error { func() error {
p.logger = logger.Get(p.Config()) p.logger = logger.Init(p.Config())
return nil return nil
}, },

View File

@ -7,26 +7,20 @@ import (
"os" "os"
) )
var ( func Init(viper *viper.Viper) *zap.Logger {
logger *zap.Logger
)
func Get(viper *viper.Viper) *zap.Logger { // Create a new atomic level
if logger == nil { atomicLevel := zap.NewAtomicLevel()
// Create a new atomic level // Set initial log level, for example, info level
atomicLevel := zap.NewAtomicLevel() atomicLevel.SetLevel(mapLogLevel(viper.GetString("core.log.level")))
// Set initial log level, for example, info level // Create the logger with the atomic level
atomicLevel.SetLevel(mapLogLevel(viper.GetString("core.log.level"))) logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
// Create the logger with the atomic level zapcore.Lock(os.Stdout),
logger = zap.New(zapcore.NewCore( atomicLevel,
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), ))
zapcore.Lock(os.Stdout),
atomicLevel,
))
}
return logger return logger
} }