Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed for for mapping fields, different var for logger in zap example #581

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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