Skip to content

Commit

Permalink
Fix logrus interceptor (#588)
Browse files Browse the repository at this point in the history
* interceptors: Fix logrus example to iterate over all fields

Fixes #570

Signed-off-by: Chance Zibolski <[email protected]>

* 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 <[email protected]>

---------

Signed-off-by: Chance Zibolski <[email protected]>
  • Loading branch information
chancez authored Jun 7, 2023
1 parent e4c2f09 commit 2aace1a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion interceptors/logging/examples/logrus/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions interceptors/logging/examples/zap/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down

0 comments on commit 2aace1a

Please sign in to comment.