Skip to content

Commit

Permalink
Modify ContextCallerSkipFrameCount in wrappers.
Browse files Browse the repository at this point in the history
For the wrappers that end up calling .Msg(), we need to increment
ContextCallerSkipFrameCount before the call to .Msg, and decrement it
afterwards.

That ensures that we always print the correct program caller.
  • Loading branch information
Zephaniah E. Loss-Cutler-Hull committed May 6, 2021
1 parent 6c3e969 commit d2f7dfa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,19 @@ func (l *Logger) Log() *Event {
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Print(v ...interface{}) {
if e := l.Debug(); e.Enabled() {
ContextCallerSkipFrameCount++
e.Msg(fmt.Sprint(v...))
ContextCallerSkipFrameCount--
}
}

// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Printf(format string, v ...interface{}) {
if e := l.Debug(); e.Enabled() {
ContextCallerSkipFrameCount++
e.Msg(fmt.Sprintf(format, v...))
ContextCallerSkipFrameCount--
}
}

Expand All @@ -412,7 +416,9 @@ func (l Logger) Write(p []byte) (n int, err error) {
// Trim CR added by stdlog.
p = p[0 : n-1]
}
ContextCallerSkipFrameCount++
l.Log().Msg(string(p))
ContextCallerSkipFrameCount--
return
}

Expand Down
4 changes: 4 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ func Log() *zerolog.Event {
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
zerolog.ContextCallerSkipFrameCount++
Logger.Print(v...)
zerolog.ContextCallerSkipFrameCount--
}

// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
zerolog.ContextCallerSkipFrameCount++
Logger.Printf(format, v...)
zerolog.ContextCallerSkipFrameCount--
}

// Ctx returns the Logger associated with the ctx. If no logger
Expand Down

0 comments on commit d2f7dfa

Please sign in to comment.