about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-07-01 22:07:53 +0200
committerAlan Pearce2024-07-01 22:07:53 +0200
commita70ddb349b0230201b58d748aec189db2f1a5608 (patch)
tree8ad9cbea35ec9208d0c4ada63178bce5633c2338
parent61dffc8ded60655776dbc2b88cfddf282de4e05f (diff)
downloadx-a70ddb349b0230201b58d748aec189db2f1a5608.tar.lz
x-a70ddb349b0230201b58d748aec189db2f1a5608.tar.zst
x-a70ddb349b0230201b58d748aec189db2f1a5608.zip
log: add logger.With and SetLevel
-rw-r--r--log/log.go20
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)
+}