logger: add TimeFunc option
All checks were successful
pr / test (pull_request) Successful in 1m36s
lint / lint (pull_request) Successful in 10m45s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2024-03-07 00:02:00 +03:00
parent b519b61fff
commit 60e5e42167
3 changed files with 27 additions and 18 deletions

View File

@@ -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"