From 5465cb5315da8ed2f5ca64ceba14163c9fa7a975 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 30 Nov 2024 11:41:32 +0100 Subject: Enable debug in production --- log/log.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/log/log.go b/log/log.go index 730c2ef..63c6b23 100644 --- a/log/log.go +++ b/log/log.go @@ -64,7 +64,6 @@ func getLevelFromEnv() (zapcore.Level, error) { var atom zap.AtomicLevel func Configure(isProduction bool) *Logger { - var l *zap.Logger atom = zap.NewAtomicLevel() level, err := getLevelFromEnv() if err != nil { @@ -72,30 +71,32 @@ func Configure(isProduction bool) *Logger { } atom.SetLevel(level) + var filter zapfilter.FilterFunc + if debug := os.Getenv("DEBUG"); debug != "" { + filter = zapfilter.Any(zapfilter.MinimumLevel(level), zapfilter.ByNamespaces(debug)) + } else { + filter = zapfilter.MinimumLevel(level) + } + + var cfg zapcore.EncoderConfig + var enc zapcore.Encoder if isProduction { - cfg := zap.NewProductionEncoderConfig() - cfg.TimeKey = "" - l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, atom)) + cfg = zap.NewProductionEncoderConfig() + enc = zaplogfmt.NewEncoder(cfg) } else { - cfg := prettyconsole.NewEncoderConfig() - cfg.TimeKey = "" - var filter zapfilter.FilterFunc - if debug := os.Getenv("DEBUG"); debug != "" { - filter = zapfilter.Any(zapfilter.MinimumLevel(level), zapfilter.ByNamespaces(debug)) - } else { - filter = zapfilter.MinimumLevel(level) - } - l = zap.New(zapfilter.NewFilteringCore( - zapcore.NewCore(prettyconsole.NewEncoder(cfg), os.Stderr, zap.DebugLevel), - filter, - )) + cfg = prettyconsole.NewEncoderConfig() + enc = prettyconsole.NewEncoder(cfg) } + cfg.TimeKey = "" + log := zap.New( + zapfilter.NewFilteringCore(zapcore.NewCore(enc, os.Stderr, zap.DebugLevel), filter), + ) - zap.ReplaceGlobals(l) - zap.RedirectStdLog(l) + zap.ReplaceGlobals(log) + zap.RedirectStdLog(log) return &Logger{ - logger: l.WithOptions(zap.AddCallerSkip(1)).Sugar(), + logger: log.WithOptions(zap.AddCallerSkip(1)).Sugar(), } } -- cgit 1.4.1