From a70ddb349b0230201b58d748aec189db2f1a5608 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 1 Jul 2024 22:07:53 +0200 Subject: log: add logger.With and SetLevel --- log/log.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'log') 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) +} -- cgit 1.4.1