Skip to content

Commit

Permalink
promlog: Fix caller (#334)
Browse files Browse the repository at this point in the history
* 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 <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie authored Oct 20, 2021
1 parent ba7ab70 commit 5e85cde
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions promlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -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)
}

0 comments on commit 5e85cde

Please sign in to comment.