Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
fix: drop sugar. only use logger
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Apr 17, 2024
1 parent a38c395 commit 5bdb2fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
39 changes: 12 additions & 27 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -215,15 +201,14 @@ 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
}

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
)
Expand Down
16 changes: 16 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 5bdb2fb

Please sign in to comment.