Skip to content

Commit

Permalink
fix(log): fix caller skip depth
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jan 28, 2025
1 parent 83ed95f commit c3eb734
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
8 changes: 6 additions & 2 deletions log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ func GetLoggerFromContext(ctx context.Context) *Logger {

func GetLoggerFromContextOrDefault(ctx context.Context) *Logger {
if logger := GetLoggerFromContext(ctx); logger != nil {
return logger
return logger.AddCallerSkip(1)
}
return globalLogger
return globalLogger.AddCallerSkip(1)
}

func UpdateContext(ctx context.Context, f func(logger *Logger) *Logger) context.Context {
return AttachLoggerToContext(ctx, f(GetLoggerFromContextOrDefault(ctx)))
}

func Tracefc(ctx context.Context, format string, args ...any) {
Expand Down
9 changes: 5 additions & 4 deletions log/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ func TestContextLogger(t *testing.T) {
SetOutput(DefaultOutput)
})

l := NewWithOutput(w).SetPrefix("test")
l := NewWithOutput(w).SetPrefix("prefix")
ctx := AttachLoggerToContext(context.Background(), l)

Infofc(ctx, "hoge %s", "fuga")
Infofc(context.Background(), "hoge %s", "fuga2")
//nolint:staticcheck // test nil context
Infofc(nil, "hoge %s", "fuga3")

scanner := bufio.NewScanner(w)
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "test\thoge fuga")
assert.Contains(t, scanner.Text(), "\tprefix\t")
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "hoge fuga2")
assert.NotContains(t, scanner.Text(), "test")
assert.NotContains(t, scanner.Text(), "\tprefix\t")
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "hoge fuga3")
assert.NotContains(t, scanner.Text(), "test")
assert.NotContains(t, scanner.Text(), "\tprefix\t")
assert.False(t, scanner.Scan())
}
4 changes: 2 additions & 2 deletions log/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func TestEcho(t *testing.T) {
w := &bytes.Buffer{}
l := NewEcho()
l.SetOutput(w)
l.SetPrefix("test")
l.SetPrefix("prefix")
l.Infof("hoge %s", "fuga")

scanner := bufio.NewScanner(w)
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "test\thoge fuga")
assert.Contains(t, scanner.Text(), "\tprefix\t")
assert.False(t, scanner.Scan())
}
20 changes: 20 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ func (l *Logger) ClearPrefix() *Logger {
}
}

func (l *Logger) WithCaller(enabled bool) *Logger {
return &Logger{
logger: l.logger.WithOptions(zap.WithCaller(enabled)),
atom: l.atom,
prefix: l.prefix,
dynPrefix: l.dynPrefix,
dynSuffix: l.dynSuffix,
}
}

func (l *Logger) AddCallerSkip(skip int) *Logger {
return &Logger{
logger: l.logger.WithOptions(zap.AddCallerSkip(skip)),
atom: l.atom,
prefix: l.prefix,
dynPrefix: l.dynPrefix,
dynSuffix: l.dynSuffix,
}
}

func (l *Logger) Debugf(format string, args ...any) {
f := l.format(format, args...)
l.logger.Debugf(f.Format, f.Args...)
Expand Down
4 changes: 2 additions & 2 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func TestLogger_SetPrefix(t *testing.T) {

scanner := bufio.NewScanner(w)
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "test\thoge fuga")
assert.Regexp(t, `\ttest\t.+?\thoge fuga$`, scanner.Text()) // .+? is a caller
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "test\t[fuga 1]")
assert.Regexp(t, `\ttest\t.+?\t\[fuga 1\]$`, scanner.Text())
assert.True(t, scanner.Scan())
assert.Contains(t, scanner.Text(), "test")
assert.Contains(t, scanner.Text(), "abcd")
Expand Down

0 comments on commit c3eb734

Please sign in to comment.