Skip to content

Commit

Permalink
fix: correct caller when using WithFields
Browse files Browse the repository at this point in the history
  • Loading branch information
piavgh committed Jan 18, 2024
1 parent 8076e92 commit f2651f6
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func getZapLevel(level string) zapcore.Level {
}

func newZapLogger(config Configuration) (*zapLogger, error) {
cores := []zapcore.Core{}
var cores []zapcore.Core
atom := zap.NewAtomicLevel()
if config.EnableConsole {
level := getZapLevel(config.ConsoleLevel)
Expand Down Expand Up @@ -129,8 +129,12 @@ func (l *zapLogger) WithFields(fields Fields) Logger {
fds = append(fds, k)
fds = append(fds, v)
}
newLogger := l.sugaredLogger.With(fds...)
return &zapLogger{newLogger, l.atomicLevel}
// AddCallerSkip(-1) is important here, else the file that gets logged will always be the wrapped file.
newLogger := l.sugaredLogger.WithOptions(zap.AddCallerSkip(-1)).With(fds...)
return &zapLogger{
sugaredLogger: newLogger,
atomicLevel: l.atomicLevel,
}
}

func (l *zapLogger) GetDelegate() interface{} {
Expand Down

0 comments on commit f2651f6

Please sign in to comment.