From 69b44047396f34142e77d6aedd75535c526dc638 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Fri, 11 Oct 2024 09:06:40 +0300 Subject: [PATCH] removed skipCallers --- logger/logger.go | 2 -- logger/options.go | 17 ----------------- logger/slog/slog.go | 22 +++++++++++++--------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 325628ac..aac0734c 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -14,8 +14,6 @@ var ( DefaultLogger Logger = NewLogger() // DefaultLevel used by logger DefaultLevel = InfoLevel - // DefaultCallerSkipCount used by logger - DefaultCallerSkipCount = 3 ) // Logger is a generic logging interface diff --git a/logger/options.go b/logger/options.go index 1320b793..bcf31142 100644 --- a/logger/options.go +++ b/logger/options.go @@ -23,8 +23,6 @@ type Options struct { Name string // Fields holds additional metadata Fields []interface{} - // CallerSkipCount number of frmaes to skip - CallerSkipCount int // ContextAttrFuncs contains funcs that executed before log func on context ContextAttrFuncs []ContextAttrFunc // TimeKey is the key used for the time of the log call @@ -57,7 +55,6 @@ func NewOptions(opts ...Option) Options { Level: DefaultLevel, Fields: make([]interface{}, 0, 6), Out: os.Stderr, - CallerSkipCount: DefaultCallerSkipCount, Context: context.Background(), ContextAttrFuncs: DefaultContextAttrFuncs, 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 func WithContext(ctx context.Context) Option { return func(o *Options) { @@ -194,10 +184,3 @@ func WithMicroKeys() Option { o.ErrorKey = "error" } } - -// WithAddCallerSkipCount add skip count for copy logger -func WithAddCallerSkipCount(n int) Option { - return func(o *Options) { - o.CallerSkipCount += n - } -} diff --git a/logger/slog/slog.go b/logger/slog/slog.go index b2a1e8f3..d90daafd 100644 --- a/logger/slog/slog.go +++ b/logger/slog/slog.go @@ -15,7 +15,11 @@ import ( "go.unistack.org/micro/v3/tracer" ) -const badKey = "!BADKEY" +const ( + badKey = "!BADKEY" + // DefaultCallerSkipCount used by logger + defaultCallerSkipCount = 3 +) const emptyMSg = "!EMPTYMSG" 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 } 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]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) @@ -209,7 +213,7 @@ func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{} return } 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]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) @@ -236,7 +240,7 @@ func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{ return } 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]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) @@ -263,7 +267,7 @@ func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{ return } 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]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) @@ -290,7 +294,7 @@ func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{ return } 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]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) @@ -336,7 +340,7 @@ func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{ return } 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]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) @@ -364,7 +368,7 @@ func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{} return } 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]) for _, fn := range s.opts.ContextAttrFuncs { 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 - 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.Add(attrs...) _ = s.handler.Handle(ctx, r)