Skip to content

Commit

Permalink
🩹(fiberzap) Fix incorrect log depth when use log.WithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Oct 7, 2023
1 parent 69e266a commit b0f56c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 12 additions & 4 deletions fiberzap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ type LoggerConfig struct {

// WithContext returns a new LoggerConfig with extra fields from context
func (l *LoggerConfig) WithContext(ctx context.Context) fiberlog.CommonLogger {
// create a new LoggerConfig with the same logger
loggerOptions := l.logger.WithOptions(zap.AddCallerSkip(-1))
newLogger := &LoggerConfig{logger: loggerOptions}

if len(l.ExtraKeys) > 0 {
newLogger := &LoggerConfig{logger: l.logger}
sugar := l.logger.Sugar()
for _, k := range l.ExtraKeys {
value := ctx.Value(k)
sugar = sugar.With(k, value)
}
// assign the new sugar to the new LoggerConfig
newLogger.logger = sugar.Desugar()
return newLogger
}
return l
return newLogger
}

type CoreConfig struct {
Expand All @@ -68,6 +68,10 @@ var LoggerConfigDefault = LoggerConfig{
LevelEncoder: zap.NewAtomicLevelAt(zap.InfoLevel),
},
},
ZapOptions: []zap.Option{
zap.AddCaller(),
zap.AddCallerSkip(3),
},
}

func loggerConfigDefault(config ...LoggerConfig) LoggerConfig {
Expand All @@ -87,6 +91,10 @@ func loggerConfigDefault(config ...LoggerConfig) LoggerConfig {
cfg.logger = cfg.SetLogger
}

if cfg.ZapOptions == nil {
cfg.ZapOptions = LoggerConfigDefault.ZapOptions
}

// Remove duplicated extraKeys
for _, k := range cfg.ExtraKeys {
if !contains(k, cfg.ExtraKeys) {
Expand Down
22 changes: 18 additions & 4 deletions fiberzap/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,37 @@ func TestZapOptions(t *testing.T) {
assert.True(t, strings.Contains(buf.String(), "caller"))
}

func TestWithContextCaller(t *testing.T) {
buf := new(bytes.Buffer)
logger := NewLogger(LoggerConfig{
ZapOptions: []zap.Option{
zap.AddCaller(),
zap.AddCallerSkip(3),
},
})
logger.SetOutput(buf)

logger.WithContext(context.Background()).Info("Hello, World!")
var logStructMap map[string]interface{}
err := json.Unmarshal(buf.Bytes(), &logStructMap)
assert.Nil(t, err)
value := logStructMap["caller"]
assert.Equal(t, value, "fiberzap/logger_test.go:180")
}

// TestWithExtraKeys test WithExtraKeys option
func TestWithExtraKeys(t *testing.T) {
buf := new(bytes.Buffer)

logger := NewLogger(LoggerConfig{
ExtraKeys: []string{"requestId"},
})
logger.SetOutput(buf)

ctx := context.WithValue(context.Background(), "requestId", "123")

logger.WithContext(ctx).Infof("%s logger", "extra")

var logStructMap map[string]interface{}

err := json.Unmarshal(buf.Bytes(), &logStructMap)

assert.Nil(t, err)

value, ok := logStructMap["requestId"]
Expand Down

0 comments on commit b0f56c3

Please sign in to comment.