From f0f14c101b7c37b8a4e7f90de1e53f8b3a2313b7 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 19 Jun 2024 14:10:50 +0200 Subject: log: enable setting log level from environment variable --- internal/log/log.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/log/log.go b/internal/log/log.go index e16d7bb..f5b48b1 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -33,15 +33,28 @@ func Fatal(msg string, rest ...any) { logger.Fatalw(msg, rest...) } +func getLevelFromEnv() (zapcore.Level, error) { + if str, found := os.LookupEnv("LOG_LEVEL"); found { + l, err := zap.ParseAtomicLevel(str) + return l.Level(), err + } + return zap.InfoLevel, nil +} + func Configure(isProduction bool) { var l *zap.Logger + level, err := getLevelFromEnv() + if err != nil { + panic(err) + } if isProduction { cfg := zap.NewProductionEncoderConfig() cfg.TimeKey = "" - l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, zapcore.InfoLevel)) + l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, level)) } else { cfg := prettyconsole.NewConfig() cfg.EncoderConfig.TimeKey = "" + cfg.Level.SetLevel(level) l = zap.Must(cfg.Build()) } logger = l.WithOptions(zap.AddCallerSkip(1)).Sugar() -- cgit 1.4.1