From 5e85cde65ae090f3dac0441c1d43fa5d392e3755 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Wed, 20 Oct 2021 09:55:30 +0200 Subject: [PATCH] promlog: Fix caller (#334) * Fix caller DefaultCaller is hard-coded to select the 3rd stack frame up, but we first filter by level, which means we need to take a 4th frame. Signed-off-by: Julien Pivotto --- promlog/log.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/promlog/log.go b/promlog/log.go index b7c7608f..e2915f3b 100644 --- a/promlog/log.go +++ b/promlog/log.go @@ -118,10 +118,11 @@ func New(config *Config) log.Logger { l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) } - l = log.With(l, "ts", timestampFormat, "caller", log.DefaultCaller) - if config.Level != nil { + l = log.With(l, "ts", timestampFormat, "caller", log.Caller(5)) l = level.NewFilter(l, config.Level.o) + } else { + l = log.With(l, "ts", timestampFormat, "caller", log.DefaultCaller) } return l } @@ -136,15 +137,16 @@ func NewDynamic(config *Config) *logger { } else { l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) } - l = log.With(l, "ts", timestampFormat, "caller", log.DefaultCaller) lo := &logger{ base: l, leveled: l, } + if config.Level != nil { lo.SetLevel(config.Level) } + return lo } @@ -166,11 +168,15 @@ func (l *logger) Log(keyvals ...interface{}) error { func (l *logger) SetLevel(lvl *AllowedLevel) { l.mtx.Lock() defer l.mtx.Unlock() - if lvl != nil { - if l.currentLevel != nil && l.currentLevel.s != lvl.s { - _ = l.base.Log("msg", "Log level changed", "prev", l.currentLevel, "current", lvl) - } - l.currentLevel = lvl + if lvl == nil { + l.leveled = log.With(l.base, "ts", timestampFormat, "caller", log.DefaultCaller) + l.currentLevel = nil + return + } + + if l.currentLevel != nil && l.currentLevel.s != lvl.s { + _ = l.base.Log("msg", "Log level changed", "prev", l.currentLevel, "current", lvl) } - l.leveled = level.NewFilter(l.base, lvl.o) + l.currentLevel = lvl + l.leveled = level.NewFilter(log.With(l.base, "ts", timestampFormat, "caller", log.Caller(5)), lvl.o) }