diff --git a/go.mod b/go.mod index 14140ea..698bf21 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,14 @@ module github.com/rockbears/log -go 1.21 +go 1.24.0 require ( github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 - go.uber.org/zap v1.26.0 + go.uber.org/zap v1.27.1 ) require ( - go.uber.org/multierr v1.10.0 // indirect - golang.org/x/sys v0.15.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/sys v0.39.0 // indirect ) diff --git a/go.sum b/go.sum index 0cff41a..da88412 100644 --- a/go.sum +++ b/go.sum @@ -15,11 +15,17 @@ go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc= +go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= +golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/log_test.go b/log_test.go index 50434ee..3b22887 100644 --- a/log_test.go +++ b/log_test.go @@ -54,24 +54,6 @@ func ExampleNewLogrusWrapper() { // level=error msg="this is error" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log } -func ExampleNewStdWrapper() { - // Init the wrapper - log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true}) - log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) - // Init the context - ctx := context.Background() - ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") - ctx = context.WithValue(ctx, fieldAsset, "ExampleNewStdWrapper") - log.Debug(ctx, "this log should not be displayed") - log.Info(ctx, "this is %q", "info") - log.Warn(ctx, "this is warn") - log.Error(ctx, "this is error") - // Output: - // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is "info" - // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is warn - // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is error -} - func ExampleNewZapWrapper() { // Init the wrapper encoderCfg := zapcore.EncoderConfig{ @@ -121,7 +103,7 @@ func TestErrorWithStackTraceAndFactory(t *testing.T) { logger.ErrorWithStackTrace(ctx, errors.WithStack(fmt.Errorf("this is an error"))) } -func ExampleNewStdWrapperAndSkip() { +func ExampleNewStdWrapper() { // Init the wrapper log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true}) log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) @@ -136,9 +118,9 @@ func ExampleNewStdWrapperAndSkip() { log.Skip(fieldAsset, "ExampleNewStdWrapper") log.Info(ctx, "this log should not be displayed because is should be skipped") // Output: - // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is "info" - // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is warn - // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is error + // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is "info" + // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is warn + // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is error } func ExampleGetRegisteredFields() { diff --git a/wrapper.go b/wrapper.go index 68d1e2e..497dea3 100644 --- a/wrapper.go +++ b/wrapper.go @@ -42,33 +42,39 @@ func (l *ZapWrapper) GetLevel() Level { panic(fmt.Errorf("zap level is not handled")) } } + func (l *ZapWrapper) WithField(key string, value interface{}) { l.sugar = l.sugar.With(key, value) } + func (l *ZapWrapper) format(format string, args ...interface{}) string { - var msg = format + msg := format if len(args) > 0 { msg = fmt.Sprintf(format, args...) } return msg } + func (l *ZapWrapper) Debugf(format string, args ...interface{}) { l.sugar.Debugw(l.format(format, args...)) } + func (l *ZapWrapper) Infof(format string, args ...interface{}) { l.sugar.Infow(l.format(format, args...)) - } + func (l *ZapWrapper) Warnf(format string, args ...interface{}) { l.sugar.Warnw(l.format(format, args...)) - } + func (l *ZapWrapper) Fatalf(format string, args ...interface{}) { l.sugar.Fatalw(l.format(format, args...)) } + func (l *ZapWrapper) Errorf(format string, args ...interface{}) { l.sugar.Errorw(l.format(format, args...)) } + func (l *ZapWrapper) Panicf(format string, args ...interface{}) { l.sugar.Panicw(l.format(format, args...)) } @@ -103,9 +109,11 @@ func (l *LogrusWrapper) GetLevel() Level { panic(fmt.Errorf("logrus level %q is not handled", l.entry.Level)) } } + func (l *LogrusWrapper) WithField(key string, value interface{}) { l.entry = l.entry.WithField(key, value) } + func (l *LogrusWrapper) Debugf(format string, args ...interface{}) { if len(args) == 0 { l.entry.Debug(format) @@ -113,6 +121,7 @@ func (l *LogrusWrapper) Debugf(format string, args ...interface{}) { l.entry.Debugf(format, args...) } } + func (l *LogrusWrapper) Infof(format string, args ...interface{}) { if len(args) == 0 { l.entry.Info(format) @@ -120,6 +129,7 @@ func (l *LogrusWrapper) Infof(format string, args ...interface{}) { l.entry.Infof(format, args...) } } + func (l *LogrusWrapper) Warnf(format string, args ...interface{}) { if len(args) == 0 { l.entry.Warn(format) @@ -127,6 +137,7 @@ func (l *LogrusWrapper) Warnf(format string, args ...interface{}) { l.entry.Warnf(format, args...) } } + func (l *LogrusWrapper) Fatalf(format string, args ...interface{}) { if len(args) == 0 { l.entry.Fatal(format) @@ -134,6 +145,7 @@ func (l *LogrusWrapper) Fatalf(format string, args ...interface{}) { l.entry.Fatalf(format, args...) } } + func (l *LogrusWrapper) Errorf(format string, args ...interface{}) { if len(args) == 0 { l.entry.Error(format) @@ -141,6 +153,7 @@ func (l *LogrusWrapper) Errorf(format string, args ...interface{}) { l.entry.Errorf(format, args...) } } + func (l *LogrusWrapper) Panicf(format string, args ...interface{}) { if len(args) == 0 { l.entry.Panic(format) @@ -165,12 +178,19 @@ type TestingWrapper struct { func (l *TestingWrapper) GetLevel() Level { return LevelDebug } + func (l *TestingWrapper) WithField(key string, value interface{}) { if l.ctx == nil { l.ctx = map[string]string{} } - l.ctx[key] = fmt.Sprintf("%v", value) + switch x := value.(type) { + case string: + l.ctx[key] = x + default: + l.ctx[key] = fmt.Sprintf("%v", value) + } } + func formatCtx(ctx map[string]string) string { var keys []string for k := range ctx { @@ -185,50 +205,57 @@ func formatCtx(ctx map[string]string) string { } return s } + +func getFormatedMsg(format string, args ...interface{}) string { + var formatedMsg string + if len(args) == 0 { + formatedMsg = format + } else { + formatedMsg = fmt.Sprintf(format, args...) + } + return formatedMsg +} + func (l *TestingWrapper) log(level, format string, args ...interface{}) { // Recover function to avoid panic: Log in goroutine after test has completed defer func() { if r := recover(); r != nil { - fmt.Printf("[" + level + "] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) + fmt.Print("[" + level + "] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } }() - if len(args) == 0 { - l.t.Log("[" + level + "] " + formatCtx(l.ctx) + " " + format) - } else { - l.t.Logf("[" + level + "] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.t.Log("[" + level + "] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } + func (l *TestingWrapper) fatal(format string, args ...interface{}) { defer func() { if r := recover(); r != nil { - fmt.Printf("[FATAL] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) + fmt.Print("[FATAL] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) os.Exit(2) } }() - if len(args) == 0 { - l.t.Fatal("[FATAL] " + formatCtx(l.ctx) + " " + format) - } else { - l.t.Fatalf("[FATAL] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.t.Fatal("[FATAL] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } func (l *TestingWrapper) Debugf(format string, args ...interface{}) { l.log("DEBUG", format, args...) } + func (l *TestingWrapper) Infof(format string, args ...interface{}) { l.log("INFO", format, args...) - } + func (l *TestingWrapper) Warnf(format string, args ...interface{}) { l.log("WARN", format, args...) - } + func (l *TestingWrapper) Fatalf(format string, args ...interface{}) { l.fatal(format, args...) } + func (l *TestingWrapper) Errorf(format string, args ...interface{}) { l.log("ERROR", format, args...) } + func (l *TestingWrapper) Panicf(format string, args ...interface{}) { l.log("PANIC", format, args...) } @@ -254,6 +281,7 @@ func NewStdWrapper(opts StdWrapperOptions) WrapperFactoryFunc { func (l *StdWrapper) GetLevel() Level { return l.opts.Level } + func (l *StdWrapper) WithField(key string, value interface{}) { if l.ctx == nil { l.ctx = map[string]string{} @@ -270,50 +298,26 @@ func (l *StdWrapper) Print(s string) { } func (l *StdWrapper) Debugf(format string, args ...interface{}) { - if len(args) == 0 { - l.Print("[DEBUG] " + formatCtx(l.ctx) + " " + format) - } else { - l.Print("[DEBUG] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.Print("[DEBUG] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } func (l *StdWrapper) Infof(format string, args ...interface{}) { - if len(args) == 0 { - l.Print("[INFO] " + formatCtx(l.ctx) + " " + format) - } else { - l.Print("[INFO] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.Print("[INFO] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } func (l *StdWrapper) Warnf(format string, args ...interface{}) { - if len(args) == 0 { - l.Print("[WARN] " + formatCtx(l.ctx) + " " + format) - } else { - l.Print("[WARN] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.Print("[WARN] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } func (l *StdWrapper) Fatalf(format string, args ...interface{}) { - if len(args) == 0 { - l.Print("[FATAL] " + formatCtx(l.ctx) + " " + format) - } else { - l.Print("[FATAL] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.Print("[FATAL] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) os.Exit(1) } func (l *StdWrapper) Errorf(format string, args ...interface{}) { - if len(args) == 0 { - l.Print("[ERROR] " + formatCtx(l.ctx) + " " + format) - } else { - l.Print("[ERROR] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.Print("[ERROR] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) } func (l *StdWrapper) Panicf(format string, args ...interface{}) { - if len(args) == 0 { - l.Print("[PANIC] " + formatCtx(l.ctx) + " " + format) - } else { - l.Print("[PANIC] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...)) - } + l.Print("[PANIC] " + formatCtx(l.ctx) + " " + getFormatedMsg(format, args...)) }