logger: add TimeFunc option
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user