Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promlog: Fix caller #334

Merged
merged 3 commits into from Oct 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions promlog/log.go
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)
}