diff options
author | Alan Pearce | 2024-07-01 22:07:53 +0200 |
---|---|---|
committer | Alan Pearce | 2024-07-01 22:07:53 +0200 |
commit | a70ddb349b0230201b58d748aec189db2f1a5608 (patch) | |
tree | 8ad9cbea35ec9208d0c4ada63178bce5633c2338 | |
parent | 61dffc8ded60655776dbc2b88cfddf282de4e05f (diff) | |
download | x-a70ddb349b0230201b58d748aec189db2f1a5608.tar.lz x-a70ddb349b0230201b58d748aec189db2f1a5608.tar.zst x-a70ddb349b0230201b58d748aec189db2f1a5608.zip |
log: add logger.With and SetLevel
-rw-r--r-- | log/log.go | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/log/log.go b/log/log.go index f644e6b..c3338ea 100644 --- a/log/log.go +++ b/log/log.go @@ -41,6 +41,11 @@ func (l Logger) Named(name string) *Logger { logger: l.logger.Named(name), } } +func (l Logger) With(args ...any) *Logger { + return &Logger{ + logger: l.logger.With(args...), + } +} func (l Logger) GetLogger() *zap.Logger { return l.logger.Desugar() @@ -48,24 +53,29 @@ func (l Logger) GetLogger() *zap.Logger { func getLevelFromEnv() (zapcore.Level, error) { if str, found := os.LookupEnv("LOG_LEVEL"); found { - l, err := zap.ParseAtomicLevel(str) + l, err := zapcore.ParseLevel(str) - return l.Level(), err + return l, err } return zap.InfoLevel, nil } +var atom zap.AtomicLevel + func Configure(isProduction bool) *Logger { var l *zap.Logger + atom = zap.NewAtomicLevel() level, err := getLevelFromEnv() if err != nil { panic(err) } + atom.SetLevel(level) + if isProduction { cfg := zap.NewProductionEncoderConfig() cfg.TimeKey = "" - l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, level)) + l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, atom)) } else { cfg := prettyconsole.NewEncoderConfig() cfg.TimeKey = "" @@ -85,3 +95,7 @@ func Configure(isProduction bool) *Logger { logger: l.WithOptions(zap.AddCallerSkip(1)).Sugar(), } } + +func SetLevel(level zapcore.Level) { + atom.SetLevel(level) +} |