2020-02-07 00:35:46 +03:00
|
|
|
package logger
|
|
|
|
|
2020-02-07 00:39:08 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2020-02-21 10:57:59 +03:00
|
|
|
"io"
|
2024-03-04 23:58:31 +03:00
|
|
|
"log/slog"
|
2020-11-04 00:38:12 +03:00
|
|
|
"os"
|
2020-02-07 00:39:08 +03:00
|
|
|
)
|
2020-02-07 00:35:46 +03:00
|
|
|
|
2024-03-04 23:03:55 +03:00
|
|
|
// Option func signature
|
2020-02-07 00:35:46 +03:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Options holds logger options
|
2020-02-07 00:35:46 +03:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Out holds the output writer
|
2020-02-21 10:57:59 +03:00
|
|
|
Out io.Writer
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds exernal options
|
2020-02-07 00:35:46 +03:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name holds the logger name
|
|
|
|
Name string
|
2024-03-04 23:03:55 +03:00
|
|
|
// Fields holds additional metadata
|
|
|
|
Fields []interface{}
|
2024-03-06 00:53:20 +03:00
|
|
|
|
2021-09-30 20:32:59 +03:00
|
|
|
// CallerSkipCount number of frmaes to skip
|
|
|
|
CallerSkipCount int
|
2024-03-04 23:58:31 +03:00
|
|
|
// ContextAttrFuncs contains funcs that executed before log func on context
|
|
|
|
ContextAttrFuncs []ContextAttrFunc
|
|
|
|
// TimeKey is the key used for the time of the log call
|
|
|
|
TimeKey string
|
|
|
|
// LevelKey is the key used for the level of the log call
|
|
|
|
LevelKey string
|
2024-03-06 00:53:20 +03:00
|
|
|
// ErroreKey is the key used for the error of the log call
|
|
|
|
ErrorKey string
|
2024-03-04 23:58:31 +03:00
|
|
|
// MessageKey is the key used for the message of the log call
|
|
|
|
MessageKey string
|
|
|
|
// SourceKey is the key used for the source file and line of the log call
|
|
|
|
SourceKey string
|
|
|
|
// StacktraceKey is the key used for the stacktrace
|
|
|
|
StacktraceKey string
|
2024-03-06 00:53:20 +03:00
|
|
|
// AddStacktrace controls writing of stacktaces on error
|
|
|
|
AddStacktrace bool
|
|
|
|
// AddSource enabled writing source file and position in log
|
|
|
|
AddSource bool
|
2024-03-04 23:03:55 +03:00
|
|
|
// The logging level the logger should log
|
|
|
|
Level Level
|
2020-02-07 00:35:46 +03:00
|
|
|
}
|
2020-02-20 11:26:12 +03:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewOptions creates new options struct
|
2020-10-16 09:38:57 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
2020-11-04 00:38:12 +03:00
|
|
|
options := Options{
|
2024-03-04 23:58:31 +03:00
|
|
|
Level: DefaultLevel,
|
|
|
|
Fields: make([]interface{}, 0, 6),
|
|
|
|
Out: os.Stderr,
|
|
|
|
CallerSkipCount: DefaultCallerSkipCount,
|
|
|
|
Context: context.Background(),
|
|
|
|
ContextAttrFuncs: DefaultContextAttrFuncs,
|
2024-03-06 00:53:20 +03:00
|
|
|
AddSource: true,
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2024-03-04 23:58:31 +03:00
|
|
|
|
|
|
|
WithMicroKeys()(&options)
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2024-03-06 00:53:20 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2024-03-04 23:58:31 +03:00
|
|
|
// WithContextAttrFuncs appends default funcs for the context arrts filler
|
|
|
|
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ContextAttrFuncs = append(o.ContextAttrFuncs, fncs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 10:57:59 +03:00
|
|
|
// WithFields set default fields for the logger
|
2021-08-06 02:15:57 +03:00
|
|
|
func WithFields(fields ...interface{}) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Fields = fields
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel set default level for the logger
|
|
|
|
func WithLevel(level Level) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Level = level
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOutput set default output writer for the logger
|
|
|
|
func WithOutput(out io.Writer) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Out = out
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 00:53:20 +03:00
|
|
|
// WitAddStacktrace controls writing stacktrace on error
|
|
|
|
func WithAddStacktrace(v bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AddStacktrace = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WitAddSource controls writing source file and pos in log
|
|
|
|
func WithAddSource(v bool) Option {
|
2024-03-04 23:03:55 +03:00
|
|
|
return func(o *Options) {
|
2024-03-06 00:53:20 +03:00
|
|
|
o.AddSource = v
|
2024-03-04 23:03:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 17:11:53 +03:00
|
|
|
// WithCallerSkipCount set frame count to skip
|
|
|
|
func WithCallerSkipCount(c int) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.CallerSkipCount = c
|
2020-04-26 17:11:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:50:29 +03:00
|
|
|
// WithContext set context
|
|
|
|
func WithContext(ctx context.Context) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
2020-11-13 14:50:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// WithName sets the name
|
2021-04-27 08:32:47 +03:00
|
|
|
func WithName(n string) Option {
|
2021-01-29 14:07:35 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 23:58:31 +03:00
|
|
|
|
|
|
|
func WithZapKeys() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimeKey = "@timestamp"
|
|
|
|
o.LevelKey = "level"
|
|
|
|
o.MessageKey = "msg"
|
|
|
|
o.SourceKey = "caller"
|
|
|
|
o.StacktraceKey = "stacktrace"
|
2024-03-06 00:53:20 +03:00
|
|
|
o.ErrorKey = "error"
|
2024-03-04 23:58:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithZerologKeys() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimeKey = "time"
|
|
|
|
o.LevelKey = "level"
|
|
|
|
o.MessageKey = "message"
|
|
|
|
o.SourceKey = "caller"
|
|
|
|
o.StacktraceKey = "stacktrace"
|
2024-03-06 00:53:20 +03:00
|
|
|
o.ErrorKey = "error"
|
2024-03-04 23:58:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSlogKeys() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimeKey = slog.TimeKey
|
|
|
|
o.LevelKey = slog.LevelKey
|
|
|
|
o.MessageKey = slog.MessageKey
|
|
|
|
o.SourceKey = slog.SourceKey
|
|
|
|
o.StacktraceKey = "stacktrace"
|
2024-03-06 00:53:20 +03:00
|
|
|
o.ErrorKey = "error"
|
2024-03-04 23:58:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMicroKeys() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimeKey = "timestamp"
|
|
|
|
o.LevelKey = "level"
|
|
|
|
o.MessageKey = "msg"
|
|
|
|
o.SourceKey = "caller"
|
|
|
|
o.StacktraceKey = "stacktrace"
|
2024-03-06 00:53:20 +03:00
|
|
|
o.ErrorKey = "error"
|
2024-03-04 23:58:31 +03:00
|
|
|
}
|
|
|
|
}
|