update slog/logger #351

Merged
vtolstov merged 11 commits from devstigneev/micro:fix_attrs_logger into v3 2024-10-12 12:37:44 +03:00
3 changed files with 13 additions and 28 deletions
Showing only changes of commit 69b4404739 - Show all commits

View File

@ -14,8 +14,6 @@ var (
DefaultLogger Logger = NewLogger() DefaultLogger Logger = NewLogger()
// DefaultLevel used by logger // DefaultLevel used by logger
DefaultLevel = InfoLevel DefaultLevel = InfoLevel
// DefaultCallerSkipCount used by logger
DefaultCallerSkipCount = 3
) )
// Logger is a generic logging interface // Logger is a generic logging interface

View File

@ -23,8 +23,6 @@ type Options struct {
Name string Name string
// Fields holds additional metadata // Fields holds additional metadata
Fields []interface{} Fields []interface{}
// CallerSkipCount number of frmaes to skip
CallerSkipCount int
// ContextAttrFuncs contains funcs that executed before log func on context // ContextAttrFuncs contains funcs that executed before log func on context
ContextAttrFuncs []ContextAttrFunc ContextAttrFuncs []ContextAttrFunc
// TimeKey is the key used for the time of the log call // TimeKey is the key used for the time of the log call
@ -57,7 +55,6 @@ func NewOptions(opts ...Option) Options {
Level: DefaultLevel, Level: DefaultLevel,
Fields: make([]interface{}, 0, 6), Fields: make([]interface{}, 0, 6),
Out: os.Stderr, Out: os.Stderr,
CallerSkipCount: DefaultCallerSkipCount,
Context: context.Background(), Context: context.Background(),
ContextAttrFuncs: DefaultContextAttrFuncs, ContextAttrFuncs: DefaultContextAttrFuncs,
AddSource: true, AddSource: true,
@ -116,13 +113,6 @@ func WithAddSource(v bool) Option {
} }
} }
// WithCallerSkipCount set frame count to skip
func WithCallerSkipCount(c int) Option {
return func(o *Options) {
o.CallerSkipCount = c
}
}
// WithContext set context // WithContext set context
func WithContext(ctx context.Context) Option { func WithContext(ctx context.Context) Option {
return func(o *Options) { return func(o *Options) {
@ -194,10 +184,3 @@ func WithMicroKeys() Option {
o.ErrorKey = "error" o.ErrorKey = "error"
} }
} }
// WithAddCallerSkipCount add skip count for copy logger
func WithAddCallerSkipCount(n int) Option {
return func(o *Options) {
o.CallerSkipCount += n
}
}

View File

@ -15,7 +15,11 @@ import (
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
) )
const badKey = "!BADKEY" const (
badKey = "!BADKEY"
// DefaultCallerSkipCount used by logger
defaultCallerSkipCount = 3
)
const emptyMSg = "!EMPTYMSG" const emptyMSg = "!EMPTYMSG"
var reTrace = regexp.MustCompile(`.*/slog/logger\.go.*\n`) var reTrace = regexp.MustCompile(`.*/slog/logger\.go.*\n`)
@ -164,7 +168,7 @@ func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, att
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Logf]
r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -209,7 +213,7 @@ func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Infof]
r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelInfo, msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelInfo, msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -236,7 +240,7 @@ func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Debugf]
r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelDebug, msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelDebug, msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -263,7 +267,7 @@ func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Tracef]
r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelDebug-1, msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelDebug-1, msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -290,7 +294,7 @@ func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Errorf]
r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelError, msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelError, msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -336,7 +340,7 @@ func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Fatalf]
r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelError+1, msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelError+1, msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -364,7 +368,7 @@ func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}
return return
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(2, pcs[:]) // skip [Callers, Warnf]
r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelWarn, msg, pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelWarn, msg, pcs[0])
for _, fn := range s.opts.ContextAttrFuncs { for _, fn := range s.opts.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...) attrs = append(attrs, fn(ctx)...)
@ -437,7 +441,7 @@ func (s *slogLogger) printLog(ctx context.Context, lvl logger.Level, attrs ...in
} }
var pcs [1]uintptr var pcs [1]uintptr
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] runtime.Callers(defaultCallerSkipCount, pcs[:]) // skip [Callers, printLog, LogLvlMethod]
r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), fmt.Sprintf("%s", msg), pcs[0]) r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), fmt.Sprintf("%s", msg), pcs[0])
r.Add(attrs...) r.Add(attrs...)
_ = s.handler.Handle(ctx, r) _ = s.handler.Handle(ctx, r)