Skip to content

Commit a18e1e2

Browse files
authored
fix: Refactor logger initialization in example_test.go (#580)
This commit refactors the logger initialization in the example_test.go files for the logr, logrus, and zap interceptors. In each file, the InterceptorLogger function was modified to create a new logger instance using the original logger and additional fields passed in as arguments, rather than modifying the original logger instance. This ensures that subsequent logs using the original logger are not affected by the additional fields. The changes were made to the following files: - interceptors/logging/examples/logr/example_test.go - interceptors/logging/examples/logrus/example_test.go - interceptors/logging/examples/zap/example_test.go Signed-off-by: aimuz <[email protected]>
1 parent 32ae386 commit a18e1e2

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

interceptors/logging/examples/logr/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
// This code is simple enough to be copied and not imported.
2626
func InterceptorLogger(l logr.Logger) logging.Logger {
2727
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
28-
l = l.WithValues(fields...)
28+
l := l.WithValues(fields...)
2929
switch lvl {
3030
case logging.LevelDebug:
3131
l.V(debugVerbosity).Info(msg)

interceptors/logging/examples/logrus/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
2222
k, v := i.At()
2323
f[k] = v
2424
}
25-
l = l.WithFields(f)
25+
l := l.WithFields(f)
2626

2727
switch lvl {
2828
case logging.LevelDebug:

interceptors/logging/examples/zap/example_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func InterceptorLogger(l *zap.Logger) logging.Logger {
1818
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
1919
f := make([]zap.Field, 0, len(fields)/2)
20-
20+
2121
for i := 0; i < len(fields); i += 2 {
2222
key := fields[i]
2323
value := fields[i+1]
@@ -33,9 +33,8 @@ func InterceptorLogger(l *zap.Logger) logging.Logger {
3333
f = append(f, zap.Any(key.(string), v))
3434
}
3535
}
36-
37-
38-
logger = l.WithOptions(zap.AddCallerSkip(1)).With(f...)
36+
37+
logger := l.WithOptions(zap.AddCallerSkip(1)).With(f...)
3938

4039
switch lvl {
4140
case logging.LevelDebug:

0 commit comments

Comments
 (0)