Skip to content

Commit

Permalink
[bugfix] Entry.SkippedCallerFrames is sometimes wrong
Browse files Browse the repository at this point in the history
Entry.SkippedCallerFrames is broken after recent global logger refactoring.

This commit also fixes printing warning message when no adapter is set for global logger.
  • Loading branch information
elgopher committed Jan 22, 2022
1 parent 16879ea commit 4710cd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion logger/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (g *Global) Debug(ctx context.Context, msg string) {
}

func (g *Global) loggerWithSkippedCallerFrame(ctx context.Context) Logger {
return g.getLogger().WithSkippedCallerFrame(ctx).WithSkippedCallerFrame()
return g.getLogger().WithSkippedCallerFrame(ctx)
}

// Info logs message using globally configured logger.Adapter.
Expand Down
18 changes: 15 additions & 3 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ func TestGlobalLogging(t *testing.T) {
})

t.Run("should log warning that global adapter was not set", func(t *testing.T) {
var global logger.Global
global.Warn(ctx, message)
t.Run("Warn", func(t *testing.T) {
var global logger.Global
global.Warn(ctx, message)
})

t.Run("With", func(t *testing.T) {
var global logger.Global
global.With(ctx, "k", "v").Warn(message)
})

t.Run("WithError", func(t *testing.T) {
var global logger.Global
global.WithError(ctx, ErrSome).Warn(message)
})
})

t.Run("should log message using global adapter", func(t *testing.T) {
Expand All @@ -62,7 +74,7 @@ func TestGlobalLogging(t *testing.T) {
logger.Entry{
Level: lvl,
Message: message,
SkippedCallerFrames: 5,
SkippedCallerFrames: 4,
},
)
})
Expand Down
4 changes: 2 additions & 2 deletions logger/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type initialGlobalNoopLogger struct {
func (g *initialGlobalNoopLogger) Log(_ context.Context, entry Entry) {
if entry.Level == WarnLevel || entry.Level == ErrorLevel {
g.once.Do(func() {
const framesToSkip = 7
_, file, line, _ := runtime.Caller(framesToSkip)
framesToSkip := 3
_, file, line, _ := runtime.Caller(entry.SkippedCallerFrames + framesToSkip)
fmt.Printf("%s:%d cannot log message with level %s. Please configure the global logger.\n", file, line, entry.Level) // nolint
})
}
Expand Down

0 comments on commit 4710cd5

Please sign in to comment.