2020-02-07 05:35:46 +08:00
|
|
|
package logger
|
|
|
|
|
2020-02-06 21:39:08 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2020-02-20 23:57:59 -08:00
|
|
|
"io"
|
2024-03-04 23:58:31 +03:00
|
|
|
"log/slog"
|
2020-11-04 00:38:12 +03:00
|
|
|
"os"
|
2024-03-07 00:02:00 +03:00
|
|
|
"time"
|
2024-05-09 16:41:22 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
2020-02-06 21:39:08 +00:00
|
|
|
)
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2024-03-04 23:03:55 +03:00
|
|
|
// Option func signature
|
2020-02-07 05:35:46 +08:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Options holds logger options
|
2020-02-07 05:35:46 +08:00
|
|
|
type Options struct {
|
2024-03-04 23:58:31 +03:00
|
|
|
// 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-12-09 13:06:43 +03:00
|
|
|
// Name holds the logger name
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Out holds the output writer
|
|
|
|
Out io.Writer
|
|
|
|
// Context holds exernal options
|
|
|
|
Context context.Context
|
2024-05-09 16:41:22 +03:00
|
|
|
// Meter used to count logs for specific level
|
|
|
|
Meter meter.Meter
|
2024-12-09 13:06:43 +03:00
|
|
|
// TimeFunc used to obtain current time
|
|
|
|
TimeFunc func() time.Time
|
|
|
|
|
|
|
|
// Fields holds additional metadata
|
|
|
|
Fields []interface{}
|
|
|
|
// ContextAttrFuncs contains funcs that executed before log func on context
|
|
|
|
ContextAttrFuncs []ContextAttrFunc
|
|
|
|
|
|
|
|
// callerSkipCount number of frmaes to skip
|
|
|
|
CallerSkipCount int
|
|
|
|
// The logging level the logger should log
|
|
|
|
Level Level
|
|
|
|
// AddSource enabled writing source file and position in log
|
|
|
|
AddSource bool
|
|
|
|
// AddStacktrace controls writing of stacktaces on error
|
|
|
|
AddStacktrace bool
|
2020-02-07 05:35:46 +08:00
|
|
|
}
|
2020-02-20 08:26:12 +00: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,
|
|
|
|
Context: context.Background(),
|
|
|
|
ContextAttrFuncs: DefaultContextAttrFuncs,
|
2024-03-06 00:53:20 +03:00
|
|
|
AddSource: true,
|
2024-03-07 00:02:00 +03:00
|
|
|
TimeFunc: time.Now,
|
2024-05-09 16:41:22 +03:00
|
|
|
Meter: meter.DefaultMeter,
|
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-07-06 00:09:27 +03:00
|
|
|
// WithContextAttrFuncs appends default funcs for the context attrs filler
|
2024-03-04 23:58:31 +03:00
|
|
|
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ContextAttrFuncs = append(o.ContextAttrFuncs, fncs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 15:34:02 +03:00
|
|
|
// WithAddFields add fields for the logger
|
|
|
|
func WithAddFields(fields ...interface{}) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Fields = append(o.Fields, fields...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:57:59 -08: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-20 23:57:59 -08: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-20 23:57:59 -08: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-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:37:43 +03:00
|
|
|
// WithAddStacktrace controls writing stacktrace on error
|
2024-03-06 00:53:20 +03:00
|
|
|
func WithAddStacktrace(v bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AddStacktrace = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:37:43 +03:00
|
|
|
// WithAddSource controls writing source file and pos in log
|
2024-03-06 00:53:20 +03:00
|
|
|
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-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
|
|
|
|
2024-09-04 22:41:05 +03:00
|
|
|
// WithMeter sets the meter
|
|
|
|
func WithMeter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:02:00 +03:00
|
|
|
// WithTimeFunc sets the func to obtain current time
|
|
|
|
func WithTimeFunc(fn func() time.Time) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimeFunc = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 23:58:31 +03:00
|
|
|
func WithZapKeys() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimeKey = "@timestamp"
|
2024-12-09 13:06:43 +03:00
|
|
|
o.LevelKey = slog.LevelKey
|
|
|
|
o.MessageKey = slog.MessageKey
|
2024-03-04 23:58:31 +03:00
|
|
|
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) {
|
2024-12-09 13:06:43 +03:00
|
|
|
o.TimeKey = slog.TimeKey
|
|
|
|
o.LevelKey = slog.LevelKey
|
2024-03-04 23:58:31 +03:00
|
|
|
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"
|
2024-12-09 13:06:43 +03:00
|
|
|
o.LevelKey = slog.LevelKey
|
|
|
|
o.MessageKey = slog.MessageKey
|
2024-03-04 23:58:31 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2024-04-15 13:30:48 +03:00
|
|
|
|
|
|
|
// WithAddCallerSkipCount add skip count for copy logger
|
|
|
|
func WithAddCallerSkipCount(n int) Option {
|
|
|
|
return func(o *Options) {
|
2024-10-12 12:37:43 +03:00
|
|
|
if n > 0 {
|
|
|
|
o.CallerSkipCount += n
|
|
|
|
}
|
2024-04-15 13:30:48 +03:00
|
|
|
}
|
|
|
|
}
|