From 60e5e421670495f5888843325125717d98a7cad3 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 7 Mar 2024 00:02:00 +0300 Subject: [PATCH] logger: add TimeFunc option Signed-off-by: Vasiliy Tolstov --- logger/options.go | 12 +++++++++++- logger/slog/slog.go | 29 ++++++++++++++--------------- logger/slog/slog_test.go | 4 ++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/logger/options.go b/logger/options.go index 170ee10d..4d083d81 100644 --- a/logger/options.go +++ b/logger/options.go @@ -5,6 +5,7 @@ import ( "io" "log/slog" "os" + "time" ) // Option func signature @@ -20,7 +21,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 @@ -43,6 +43,8 @@ type Options struct { AddSource bool // The logging level the logger should log Level Level + // TimeFunc used to obtain current time + TimeFunc func() time.Time } // NewOptions creates new options struct @@ -55,6 +57,7 @@ func NewOptions(opts ...Option) Options { Context: context.Background(), ContextAttrFuncs: DefaultContextAttrFuncs, AddSource: true, + TimeFunc: time.Now, } WithMicroKeys()(&options) @@ -129,6 +132,13 @@ func WithName(n string) Option { } } +// WithTimeFunc sets the func to obtain current time +func WithTimeFunc(fn func() time.Time) Option { + return func(o *Options) { + o.TimeFunc = fn + } +} + func WithZapKeys() Option { return func(o *Options) { o.TimeKey = "@timestamp" diff --git a/logger/slog/slog.go b/logger/slog/slog.go index 483b1bc2..5095e06d 100644 --- a/logger/slog/slog.go +++ b/logger/slog/slog.go @@ -9,7 +9,6 @@ import ( "runtime" "strconv" "sync" - "time" "go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/tracer" @@ -159,7 +158,7 @@ func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interfa } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), loggerToSlogLevel(lvl), fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -198,7 +197,7 @@ func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, att } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } @@ -237,7 +236,7 @@ func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) { } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), slog.LevelInfo, fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelInfo, fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -258,7 +257,7 @@ func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{} } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } @@ -279,7 +278,7 @@ func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) { } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), slog.LevelDebug, fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelDebug, fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -300,7 +299,7 @@ func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{ } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } @@ -321,7 +320,7 @@ func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) { } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), slog.LevelDebug-1, fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelDebug-1, fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -342,7 +341,7 @@ func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{ } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } @@ -363,7 +362,7 @@ func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) { } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), slog.LevelError, fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelError, fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -402,7 +401,7 @@ func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{ } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } @@ -441,7 +440,7 @@ func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) { } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), slog.LevelError+1, fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelError+1, fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -463,7 +462,7 @@ func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{ } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } @@ -485,7 +484,7 @@ func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) { } var pcs [1]uintptr runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, Infof] - r := slog.NewRecord(time.Now(), slog.LevelWarn, fmt.Sprintf("%s", attrs[0]), pcs[0]) + r := slog.NewRecord(s.opts.TimeFunc(), slog.LevelWarn, fmt.Sprintf("%s", attrs[0]), pcs[0]) for _, fn := range s.opts.ContextAttrFuncs { attrs = append(attrs, fn(ctx)...) } @@ -506,7 +505,7 @@ func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{} } var pcs [1]uintptr 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 { attrs = append(attrs, fn(ctx)...) } diff --git a/logger/slog/slog_test.go b/logger/slog/slog_test.go index 8844f175..93c63c79 100644 --- a/logger/slog/slog_test.go +++ b/logger/slog/slog_test.go @@ -13,7 +13,7 @@ import ( func TestError(t *testing.T) { ctx := context.TODO() buf := bytes.NewBuffer(nil) - l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithStacktrace(true)) + l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithAddStacktrace(true)) if err := l.Init(); err != nil { t.Fatal(err) } @@ -30,7 +30,7 @@ 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.WithStacktrace(true)) + l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithAddStacktrace(true)) if err := l.Init(); err != nil { t.Fatal(err) } -- 2.45.2