diff options
author | Alan Pearce | 2024-06-19 14:10:50 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-19 14:10:50 +0200 |
commit | f0f14c101b7c37b8a4e7f90de1e53f8b3a2313b7 (patch) | |
tree | 81a336b95402c221fc2b34d68fa11c1a97b24840 /internal | |
parent | 8002766378b1153e749297ed1d01ba221ee72b41 (diff) | |
download | website-f0f14c101b7c37b8a4e7f90de1e53f8b3a2313b7.tar.lz website-f0f14c101b7c37b8a4e7f90de1e53f8b3a2313b7.tar.zst website-f0f14c101b7c37b8a4e7f90de1e53f8b3a2313b7.zip |
log: enable setting log level from environment variable
Diffstat (limited to 'internal')
-rw-r--r-- | internal/log/log.go | 15 |
1 files changed, 14 insertions, 1 deletions
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() |