From 5bdb2fb9a76b7c247396c73b2d05f246fde181dd Mon Sep 17 00:00:00 2001 From: thinkgos Date: Wed, 17 Apr 2024 20:47:24 +0800 Subject: [PATCH] fix: drop sugar. only use logger --- logger.go | 39 ++++++++++++--------------------------- logger_test.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/logger.go b/logger.go index 65c22ec..0f34977 100644 --- a/logger.go +++ b/logger.go @@ -141,43 +141,30 @@ func (l *Log) Sync() error { } func (l *Log) Log(ctx context.Context, level Level, args ...any) { - if !l.level.Enabled(level) { - return - } - if len(l.fn) == 0 { - l.Sugar().Log(level, args...) - } else { - l.Logx(ctx, level, getMessage("", args)) - } + l.Logx(ctx, level, formatMessage("", args)) } func (l *Log) Logf(ctx context.Context, level Level, template string, args ...any) { - if !l.level.Enabled(level) { - return - } - if len(l.fn) == 0 { - l.Sugar().Logf(level, template, args...) - } else { - l.Logx(ctx, level, getMessage(template, args)) - } + l.Logx(ctx, level, formatMessage(template, args)) } func (l *Log) Logw(ctx context.Context, level Level, msg string, keysAndValues ...any) { if !l.level.Enabled(level) { return } - if len(l.fn) == 0 { - l.Sugar().Logw(level, msg, keysAndValues...) - } else { - l.Logx(ctx, level, msg, l.sweetenFields(keysAndValues)...) + fc := defaultFieldPool.Get() + defer defaultFieldPool.Put(fc) + for _, f := range l.fn { + fc.Fields = append(fc.Fields, f(ctx)) } + fc.Fields = l.appendSweetenFields(fc.Fields, keysAndValues) + l.log.Log(level, msg, fc.Fields...) } func (l *Log) Logx(ctx context.Context, level Level, msg string, fields ...Field) { if !l.level.Enabled(level) { return } - if len(l.fn) == 0 { l.log.Log(level, msg, fields...) } else { @@ -197,16 +184,15 @@ const ( _multipleErrMsg = "Multiple errors without a key." ) -// getMessage format with Sprint, Sprintf, or neither. -func getMessage(template string, fmtArgs []interface{}) string { +// formatMessage format with Sprint, Sprintf, or neither. +// copy from zap(sugar.go) +func formatMessage(template string, fmtArgs []interface{}) string { if len(fmtArgs) == 0 { return template } - if template != "" { return fmt.Sprintf(template, fmtArgs...) } - if len(fmtArgs) == 1 { if str, ok := fmtArgs[0].(string); ok { return str @@ -215,7 +201,7 @@ func getMessage(template string, fmtArgs []interface{}) string { return fmt.Sprint(fmtArgs...) } -func (l *Log) sweetenFields(args []interface{}) []Field { +func (l *Log) appendSweetenFields(fields []Field, args []interface{}) []Field { if len(args) == 0 { return nil } @@ -223,7 +209,6 @@ func (l *Log) sweetenFields(args []interface{}) []Field { var ( // Allocate enough space for the worst case; if users pass only structured // fields, we shouldn't penalize them with extra allocations. - fields = make([]Field, 0, len(args)) invalid invalidPairs seenError bool ) diff --git a/logger_test.go b/logger_test.go index 18ca6f6..4a17582 100644 --- a/logger_test.go +++ b/logger_test.go @@ -24,6 +24,14 @@ func Test_LoggerNormal(t *testing.T) { log.Warn("Warn") log.Error("Error") log.DPanic("DPanic") + + // no hook + l := log.WithNewValuer() + l.Debug("Debug") + l.Info("Info") + l.Warn("Warn") + l.Error("Error") + l.DPanic("DPanic") } func Test_LoggerFormater(t *testing.T) { @@ -32,6 +40,14 @@ func Test_LoggerFormater(t *testing.T) { log.Warnf("Warnf: %s", "warn") log.Errorf("Errorf: %s", "error") log.DPanicf("DPanicf: %s", "dPanic") + + // no hook + l := log.WithNewValuer() + l.Debugf("Debugf: %s", "debug") + l.Infof("Infof: %s", "info") + l.Warnf("Warnf: %s", "warn") + l.Errorf("Errorf: %s", "error") + l.DPanicf("DPanicf: %s", "dPanic") } func Test_LoggerKeyValue(t *testing.T) {