From c90d5cb62b2d280c1a564a1af95c3eb7303f6514 Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Tue, 6 Jun 2023 10:05:11 -0700 Subject: [PATCH 1/2] interceptors: Fix logrus example to iterate over all fields Fixes https://github.com/grpc-ecosystem/go-grpc-middleware/issues/570 Signed-off-by: Chance Zibolski --- interceptors/logging/examples/logrus/example_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interceptors/logging/examples/logrus/example_test.go b/interceptors/logging/examples/logrus/example_test.go index beb8ad843..1cb38ebf0 100644 --- a/interceptors/logging/examples/logrus/example_test.go +++ b/interceptors/logging/examples/logrus/example_test.go @@ -18,7 +18,7 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger { return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { f := make(map[string]any, len(fields)/2) i := logging.Fields(fields).Iterator() - if i.Next() { + for i.Next() { k, v := i.At() f[k] = v } From 3b2f1060961e324d9ce9154bf080b15636dce721 Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Tue, 6 Jun 2023 10:07:09 -0700 Subject: [PATCH 2/2] interceptors: Update zap InterceptorLogger to use Iterator properly The logic in the example is redundant with the logging.Fields Iterator Signed-off-by: Chance Zibolski --- interceptors/logging/examples/zap/example_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/interceptors/logging/examples/zap/example_test.go b/interceptors/logging/examples/zap/example_test.go index 88a7b0d77..1b682d5a0 100644 --- a/interceptors/logging/examples/zap/example_test.go +++ b/interceptors/logging/examples/zap/example_test.go @@ -17,12 +17,10 @@ import ( func InterceptorLogger(l *zap.Logger) logging.Logger { return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) { f := make([]zap.Field, 0, len(fields)/2) - for i := 0; i < len(fields); i += 2 { - i := logging.Fields(fields).Iterator() - if i.Next() { - k, v := i.At() - f = append(f, zap.Any(k, v)) - } + i := logging.Fields(fields).Iterator() + for i.Next() { + k, v := i.At() + f = append(f, zap.Any(k, v)) } l = l.WithOptions(zap.AddCallerSkip(1)).With(f...)