fix changes
This commit is contained in:
parent
60c3898dd6
commit
8eba189d99
@ -45,20 +45,27 @@ type Logger interface {
|
|||||||
// Fatal level message
|
// Fatal level message
|
||||||
Fatal(ctx context.Context, args ...interface{})
|
Fatal(ctx context.Context, args ...interface{})
|
||||||
// Infof level message
|
// Infof level message
|
||||||
|
// DEPRECATED
|
||||||
Infof(ctx context.Context, msg string, args ...interface{})
|
Infof(ctx context.Context, msg string, args ...interface{})
|
||||||
// Tracef level message
|
// Tracef level message
|
||||||
|
// DEPRECATED
|
||||||
Tracef(ctx context.Context, msg string, args ...interface{})
|
Tracef(ctx context.Context, msg string, args ...interface{})
|
||||||
// Debug level message
|
// Debugf level message
|
||||||
|
// DEPRECATED
|
||||||
Debugf(ctx context.Context, msg string, args ...interface{})
|
Debugf(ctx context.Context, msg string, args ...interface{})
|
||||||
// Warn level message
|
// Warnf level message
|
||||||
|
// DEPRECATED
|
||||||
Warnf(ctx context.Context, msg string, args ...interface{})
|
Warnf(ctx context.Context, msg string, args ...interface{})
|
||||||
// Error level message
|
// Errorf level message
|
||||||
|
// DEPRECATED
|
||||||
Errorf(ctx context.Context, msg string, args ...interface{})
|
Errorf(ctx context.Context, msg string, args ...interface{})
|
||||||
// Fatal level message
|
// Fatalf level message
|
||||||
|
// DEPRECATED
|
||||||
Fatalf(ctx context.Context, msg string, args ...interface{})
|
Fatalf(ctx context.Context, msg string, args ...interface{})
|
||||||
// Log logs message with needed level
|
// Log logs message with needed level
|
||||||
Log(ctx context.Context, level Level, args ...interface{})
|
Log(ctx context.Context, level Level, args ...interface{})
|
||||||
// Logf logs message with needed level
|
// Logf logs message with needed level
|
||||||
|
// DEPRECATED
|
||||||
Logf(ctx context.Context, level Level, msg string, args ...interface{})
|
Logf(ctx context.Context, level Level, msg string, args ...interface{})
|
||||||
// Name returns broker instance name
|
// Name returns broker instance name
|
||||||
Name() string
|
Name() string
|
||||||
|
@ -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 {
|
func WithAddStacktrace(v bool) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.AddStacktrace = v
|
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 {
|
func WithAddSource(v bool) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.AddSource = v
|
o.AddSource = v
|
||||||
|
@ -155,11 +155,11 @@ func (s *slogLogger) Init(opts ...logger.Option) error {
|
|||||||
|
|
||||||
func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interface{}) {
|
func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
|
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
|
||||||
if !s.V(lvl) {
|
if !s.V(lvl) || len(attrs) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
attrs = prepareAttributes(attrs)
|
//attrs = prepareAttributes(attrs)
|
||||||
|
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
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 {
|
for idx, attr := range attrs {
|
||||||
if ve, ok := attr.(error); ok && ve != nil {
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,9 +200,10 @@ func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interfa
|
|||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logf DEPRECATED
|
||||||
func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
|
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
|
||||||
if !s.V(lvl) {
|
if !s.V(lvl) || len(attrs) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
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{}) {
|
func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.InfoLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,9 +266,10 @@ func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) {
|
|||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Infof DEPRECATED
|
||||||
func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.InfoLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
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{}) {
|
func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.DebugLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,9 +314,10 @@ func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) {
|
|||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debugf DEPRECATED
|
||||||
func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.DebugLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
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{}) {
|
func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.TraceLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,9 +362,10 @@ func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) {
|
|||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tracef DEPRECATED
|
||||||
func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.TraceLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
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{}) {
|
func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.ErrorLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
attrs = prepareAttributes(attrs)
|
//attrs = prepareAttributes(attrs)
|
||||||
|
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof]
|
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 {
|
for idx, attr := range attrs {
|
||||||
if ve, ok := attr.(error); ok && ve != nil {
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -420,9 +432,10 @@ func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) {
|
|||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Errorf DEPRECATED
|
||||||
func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.ErrorLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
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{}) {
|
func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.FatalLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -486,9 +499,10 @@ func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fatalf DEPRECATED
|
||||||
func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.FatalLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
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{}) {
|
func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.WarnLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -534,9 +548,10 @@ func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) {
|
|||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Warnf DEPRECATED
|
||||||
func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}) {
|
func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}) {
|
||||||
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.WarnLevel.String()).Inc()
|
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
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
@ -607,9 +622,7 @@ func slogToLoggerLevel(level slog.Level) logger.Level {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func prepareAttributes(attrs []interface{}) []interface{} {
|
func prepareAttributes(attrs []interface{}) []interface{} {
|
||||||
if len(attrs) == 0 {
|
if len(attrs) == 1 {
|
||||||
return append(attrs, emptyMSg)
|
|
||||||
} else if len(attrs) == 1 {
|
|
||||||
return attrs
|
return attrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package slog
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
@ -29,13 +30,18 @@ func TestError(t *testing.T) {
|
|||||||
|
|
||||||
func TestErrorf(t *testing.T) {
|
func TestErrorf(t *testing.T) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithAddStacktrace(true))
|
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)
|
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":"`)) {
|
if !bytes.Contains(buf.Bytes(), []byte(`"stacktrace":"`)) {
|
||||||
t.Fatalf("logger stacktrace not works, buf contains: %s", buf.Bytes())
|
t.Fatalf("logger stacktrace not works, buf contains: %s", buf.Bytes())
|
||||||
}
|
}
|
||||||
|
@ -224,11 +224,11 @@ func (l *lWrapper) Call(ctx context.Context, req client.Request, rsp interface{}
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var labels []string
|
var labels []interface{}
|
||||||
for _, o := range l.opts.ClientCallObservers {
|
for _, o := range l.opts.ClientCallObservers {
|
||||||
labels = append(labels, o(ctx, req, rsp, opts, err)...)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user