logger: add TimeFunc option
Some checks failed
/ autoupdate (push) Failing after 1m3s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-03-07 00:08:34 +03:00
parent 7ef5c5d804
commit 4c3c4859fc
2 changed files with 11 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"reflect" "reflect"
"time"
"go.unistack.org/micro/v4/options" "go.unistack.org/micro/v4/options"
rutil "go.unistack.org/micro/v4/util/reflect" rutil "go.unistack.org/micro/v4/util/reflect"
@ -43,6 +44,8 @@ type Options struct {
AddStacktrace bool AddStacktrace bool
// AddSource enabled writing source file and position in log // AddSource enabled writing source file and position in log
AddSource bool AddSource bool
// TimeFunc used to obtain current time
TimeFunc func() time.Time
} }
// NewOptions creates new options struct // NewOptions creates new options struct
@ -55,6 +58,7 @@ func NewOptions(opts ...options.Option) Options {
Context: context.Background(), Context: context.Background(),
ContextAttrFuncs: DefaultContextAttrFuncs, ContextAttrFuncs: DefaultContextAttrFuncs,
AddSource: true, AddSource: true,
TimeFunc: time.Now,
} }
_ = WithMicroKeys()(&options) _ = WithMicroKeys()(&options)

View File

@ -8,7 +8,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
"time"
"go.unistack.org/micro/v4/logger" "go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/options" "go.unistack.org/micro/v4/options"
@ -160,7 +159,7 @@ func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, msg string, attr
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }
@ -198,7 +197,7 @@ func (s *slogLogger) Info(ctx context.Context, msg string, attrs ...interface{})
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }
@ -212,7 +211,7 @@ func (s *slogLogger) Debug(ctx context.Context, msg string, attrs ...interface{}
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }
@ -226,7 +225,7 @@ func (s *slogLogger) Trace(ctx context.Context, msg string, attrs ...interface{}
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }
@ -240,7 +239,7 @@ func (s *slogLogger) Error(ctx context.Context, msg string, attrs ...interface{}
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }
@ -278,7 +277,7 @@ func (s *slogLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }
@ -293,7 +292,7 @@ func (s *slogLogger) Warn(ctx context.Context, msg string, attrs ...interface{})
} }
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]
r := slog.NewRecord(time.Now(), 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)...)
} }