diff options
author | Alan Pearce | 2024-11-30 11:41:32 +0100 |
---|---|---|
committer | Alan Pearce | 2024-11-30 11:41:32 +0100 |
commit | 5465cb5315da8ed2f5ca64ceba14163c9fa7a975 (patch) | |
tree | 563b6047f8ac395bf06541d3325dfe69bc84826f /log/log.go | |
parent | 00de54be50ebf19913eb9ee37fa4de015bf5475b (diff) | |
download | x-5465cb5315da8ed2f5ca64ceba14163c9fa7a975.tar.lz x-5465cb5315da8ed2f5ca64ceba14163c9fa7a975.tar.zst x-5465cb5315da8ed2f5ca64ceba14163c9fa7a975.zip |
Enable debug in production
Diffstat (limited to 'log/log.go')
-rw-r--r-- | log/log.go | 39 |
1 files 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(), } } |