Skip to content

Commit

Permalink
Changed for for mapping fields, different var for logger in zap examp…
Browse files Browse the repository at this point in the history
…le (#581)

* Mapping Fields works correctly now
* Logger instance used for logging is now being used only once, previous code has been rewriting instance, and then duplicating fields.
  • Loading branch information
MichalFikejs committed May 15, 2023
1 parent cb96b57 commit b9ea828
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions interceptors/logging/examples/zap/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@ 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))
key := fields[i]
value := fields[i+1]

switch v := value.(type) {
case string:
f = append(f, zap.String(key.(string), v))
case int:
f = append(f, zap.Int(key.(string), v))
case bool:
f = append(f, zap.Bool(key.(string), v))
default:
f = append(f, zap.Any(key.(string), v))
}
}
l = l.WithOptions(zap.AddCallerSkip(1)).With(f...)


logger = l.WithOptions(zap.AddCallerSkip(1)).With(f...)

switch lvl {
case logging.LevelDebug:
l.Debug(msg)
logger.Debug(msg)
case logging.LevelInfo:
l.Info(msg)
logger.Info(msg)
case logging.LevelWarn:
l.Warn(msg)
logger.Warn(msg)
case logging.LevelError:
l.Error(msg)
logger.Error(msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
Expand Down

0 comments on commit b9ea828

Please sign in to comment.