From 8eba189d99b145bda92ba3b14c09224a7878408b Mon Sep 17 00:00:00 2001 From: devstigneev Date: Tue, 8 Oct 2024 21:04:58 +0300 Subject: [PATCH] fix changes --- logger/logger.go | 15 ++++++++--- logger/options.go | 4 +-- logger/slog/slog.go | 55 ++++++++++++++++++++++++--------------- logger/slog/slog_test.go | 10 +++++-- logger/wrapper/wrapper.go | 4 +-- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 175311bc..20a5d01f 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -45,20 +45,27 @@ type Logger interface { // Fatal level message Fatal(ctx context.Context, args ...interface{}) // Infof level message + // DEPRECATED Infof(ctx context.Context, msg string, args ...interface{}) // Tracef level message + // DEPRECATED Tracef(ctx context.Context, msg string, args ...interface{}) - // Debug level message + // Debugf level message + // DEPRECATED Debugf(ctx context.Context, msg string, args ...interface{}) - // Warn level message + // Warnf level message + // DEPRECATED Warnf(ctx context.Context, msg string, args ...interface{}) - // Error level message + // Errorf level message + // DEPRECATED Errorf(ctx context.Context, msg string, args ...interface{}) - // Fatal level message + // Fatalf level message + // DEPRECATED Fatalf(ctx context.Context, msg string, args ...interface{}) // Log logs message with needed level Log(ctx context.Context, level Level, args ...interface{}) // Logf logs message with needed level + // DEPRECATED Logf(ctx context.Context, level Level, msg string, args ...interface{}) // Name returns broker instance name Name() string diff --git a/logger/options.go b/logger/options.go index e1113daf..1320b793 100644 --- a/logger/options.go +++ b/logger/options.go @@ -102,14 +102,14 @@ func WithOutput(out io.Writer) Option { } } -// WitAddStacktrace controls writing stacktrace on error +// WithAddStacktrace controls writing stacktrace on error func WithAddStacktrace(v bool) Option { return func(o *Options) { o.AddStacktrace = v } } -// WitAddSource controls writing source file and pos in log +// WithAddSource controls writing source file and pos in log func WithAddSource(v bool) Option { return func(o *Options) { o.AddSource = v diff --git a/logger/slog/slog.go b/logger/slog/slog.go index c703955f..91cda9e2 100644 --- a/logger/slog/slog.go +++ b/logger/slog/slog.go @@ -155,11 +155,11 @@ func (s *slogLogger) Init(opts ...logger.Option) error { func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc() - if !s.V(lvl) { + if !s.V(lvl) || len(attrs) == 0 { return } - attrs = prepareAttributes(attrs) + //attrs = prepareAttributes(attrs) var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] @@ -170,7 +170,11 @@ func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interfa for idx, attr := range attrs { if ve, ok := attr.(error); ok && ve != nil { - attrs[idx] = slog.String(s.opts.ErrorKey, ve.Error()) + if idx%2 == 1 { + attrs = append(attrs, slog.String(s.opts.ErrorKey, ve.Error())) + } else { + attrs[idx] = slog.String(s.opts.ErrorKey, ve.Error()) + } break } } @@ -196,9 +200,10 @@ func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interfa _ = s.handler.Handle(ctx, r) } +// Logf DEPRECATED func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc() - if !s.V(lvl) { + if !s.V(lvl) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -238,7 +243,7 @@ func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, att func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.InfoLevel.String()).Inc() - if !s.V(logger.InfoLevel) { + if !s.V(logger.InfoLevel) || len(attrs) == 0 { return } @@ -261,9 +266,10 @@ func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) { _ = s.handler.Handle(ctx, r) } +// Infof DEPRECATED func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.InfoLevel.String()).Inc() - if !s.V(logger.InfoLevel) { + if !s.V(logger.InfoLevel) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -285,7 +291,7 @@ func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{} func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.DebugLevel.String()).Inc() - if !s.V(logger.DebugLevel) { + if !s.V(logger.DebugLevel) || len(attrs) == 0 { return } @@ -308,9 +314,10 @@ func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) { _ = s.handler.Handle(ctx, r) } +// Debugf DEPRECATED func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.DebugLevel.String()).Inc() - if !s.V(logger.DebugLevel) { + if !s.V(logger.DebugLevel) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -332,7 +339,7 @@ func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{ func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.TraceLevel.String()).Inc() - if !s.V(logger.TraceLevel) { + if !s.V(logger.TraceLevel) || len(attrs) == 0 { return } @@ -355,9 +362,10 @@ func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) { _ = s.handler.Handle(ctx, r) } +// Tracef DEPRECATED func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.TraceLevel.String()).Inc() - if !s.V(logger.TraceLevel) { + if !s.V(logger.TraceLevel) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -379,11 +387,11 @@ func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{ func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.ErrorLevel.String()).Inc() - if !s.V(logger.ErrorLevel) { + if !s.V(logger.ErrorLevel) || len(attrs) == 0 { return } - attrs = prepareAttributes(attrs) + //attrs = prepareAttributes(attrs) var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] @@ -394,7 +402,11 @@ func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) { for idx, attr := range attrs { if ve, ok := attr.(error); ok && ve != nil { - attrs[idx] = slog.String(s.opts.ErrorKey, ve.Error()) + if idx%2 == 1 { + attrs = append(attrs, slog.String(s.opts.ErrorKey, ve.Error())) + } else { + attrs[idx] = slog.String(s.opts.ErrorKey, ve.Error()) + } break } } @@ -420,9 +432,10 @@ func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) { _ = s.handler.Handle(ctx, r) } +// Errorf DEPRECATED func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.ErrorLevel.String()).Inc() - if !s.V(logger.ErrorLevel) { + if !s.V(logger.ErrorLevel) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -462,7 +475,7 @@ func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{ func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.FatalLevel.String()).Inc() - if !s.V(logger.FatalLevel) { + if !s.V(logger.FatalLevel) || len(attrs) == 0 { return } @@ -486,9 +499,10 @@ func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) { os.Exit(1) } +// Fatalf DEPRECATED func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.FatalLevel.String()).Inc() - if !s.V(logger.FatalLevel) { + if !s.V(logger.FatalLevel) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -511,7 +525,7 @@ func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{ func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.WarnLevel.String()).Inc() - if !s.V(logger.WarnLevel) { + if !s.V(logger.WarnLevel) || len(attrs) == 0 { return } @@ -534,9 +548,10 @@ func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) { _ = s.handler.Handle(ctx, r) } +// Warnf DEPRECATED func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}) { s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.WarnLevel.String()).Inc() - if !s.V(logger.WarnLevel) { + if !s.V(logger.WarnLevel) || len(attrs) == 0 { return } var pcs [1]uintptr @@ -607,9 +622,7 @@ func slogToLoggerLevel(level slog.Level) logger.Level { } func prepareAttributes(attrs []interface{}) []interface{} { - if len(attrs) == 0 { - return append(attrs, emptyMSg) - } else if len(attrs) == 1 { + if len(attrs) == 1 { return attrs } diff --git a/logger/slog/slog_test.go b/logger/slog/slog_test.go index f1b9ad44..15d85e4b 100644 --- a/logger/slog/slog_test.go +++ b/logger/slog/slog_test.go @@ -3,6 +3,7 @@ package slog import ( "bytes" "context" + "errors" "fmt" "log" "testing" @@ -29,13 +30,18 @@ func TestError(t *testing.T) { func TestErrorf(t *testing.T) { ctx := context.TODO() + buf := bytes.NewBuffer(nil) l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithAddStacktrace(true)) - if err := l.Init(); err != nil { + if err := l.Init(logger.WithContextAttrFuncs(func(ctx context.Context) []interface{} { + return nil + })); err != nil { t.Fatal(err) } - l.Errorf(ctx, "message", fmt.Errorf("error message")) + //l.Log(ctx, logger.ErrorLevel, "я вот отправил сообщение", "Это ключ для инта") + + l.Error(ctx, "message", errors.New("error_tag1")) if !bytes.Contains(buf.Bytes(), []byte(`"stacktrace":"`)) { t.Fatalf("logger stacktrace not works, buf contains: %s", buf.Bytes()) } diff --git a/logger/wrapper/wrapper.go b/logger/wrapper/wrapper.go index ee96f5c6..009845f4 100644 --- a/logger/wrapper/wrapper.go +++ b/logger/wrapper/wrapper.go @@ -224,11 +224,11 @@ func (l *lWrapper) Call(ctx context.Context, req client.Request, rsp interface{} return err } - var labels []string + var labels []interface{} for _, o := range l.opts.ClientCallObservers { labels = append(labels, o(ctx, req, rsp, opts, err)...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return err }