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"
|
2023-10-17 01:10:52 +03:00
|
|
|
"log/slog"
|
2020-11-04 00:38:12 +03:00
|
|
|
"os"
|
2023-10-16 23:43:39 +03:00
|
|
|
"reflect"
|
2020-02-07 00:35:46 +03:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-10-16 23:43:39 +03:00
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2023-07-29 00:40:58 +03:00
|
|
|
)
|
2020-02-07 00:35:46 +03:00
|
|
|
|
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
|
2023-10-16 23:43:39 +03:00
|
|
|
// Attrs holds additional attributes
|
|
|
|
Attrs []interface{}
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name holds the logger name
|
|
|
|
Name string
|
2021-09-30 20:32:59 +03:00
|
|
|
// The logging level the logger should log
|
|
|
|
Level Level
|
|
|
|
// CallerSkipCount number of frmaes to skip
|
|
|
|
CallerSkipCount int
|
2023-10-16 23:43:39 +03:00
|
|
|
// ContextAttrFuncs contains funcs that executed before log func on context
|
|
|
|
ContextAttrFuncs []ContextAttrFunc
|
2023-10-17 01:00:00 +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
|
|
|
|
// 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
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2020-11-04 00:38:12 +03:00
|
|
|
options := Options{
|
2023-10-16 23:43:39 +03:00
|
|
|
Level: DefaultLevel,
|
|
|
|
Attrs: make([]interface{}, 0, 6),
|
|
|
|
Out: os.Stderr,
|
|
|
|
CallerSkipCount: DefaultCallerSkipCount,
|
|
|
|
Context: context.Background(),
|
|
|
|
ContextAttrFuncs: DefaultContextAttrFuncs,
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
|
|
|
|
WithMicroKeys()(&options)
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
// WithContextAttrFuncs appends default funcs for the context arrts filler
|
2023-10-17 01:00:00 +03:00
|
|
|
func WithContextAttrFuncs(fncs ...ContextAttrFunc) options.Option {
|
2023-07-29 00:40:58 +03:00
|
|
|
return func(src interface{}) error {
|
2023-10-16 23:43:39 +03:00
|
|
|
v, err := options.Get(src, ".ContextAttrFuncs")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if rutil.IsZero(v) {
|
2023-10-17 01:00:00 +03:00
|
|
|
v = reflect.MakeSlice(reflect.TypeOf(v), 0, len(fncs)).Interface()
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
cv := reflect.ValueOf(v)
|
2023-10-17 01:00:00 +03:00
|
|
|
for _, l := range fncs {
|
2023-10-16 23:43:39 +03:00
|
|
|
cv = reflect.Append(cv, reflect.ValueOf(l))
|
|
|
|
}
|
|
|
|
return options.Set(src, cv.Interface(), ".ContextAttrFuncs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAttrs set default fields for the logger
|
|
|
|
func WithAttrs(attrs ...interface{}) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, attrs, ".Attrs")
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel set default level for the logger
|
2023-07-29 00:40:58 +03:00
|
|
|
func WithLevel(lvl Level) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, lvl, ".Level")
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOutput set default output writer for the logger
|
2023-07-29 00:40:58 +03:00
|
|
|
func WithOutput(out io.Writer) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, out, ".Out")
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 17:11:53 +03:00
|
|
|
// WithCallerSkipCount set frame count to skip
|
2023-07-29 00:40:58 +03:00
|
|
|
func WithCallerSkipCount(c int) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, c, ".CallerSkipCount")
|
2021-01-29 14:07:35 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
|
|
|
|
func WithZapKeys() options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
var err error
|
|
|
|
if err = options.Set(src, "@timestamp", ".TimeKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "level", ".LevelKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "msg", ".MessageKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "caller", ".SourceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithZerologKeys() options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
var err error
|
|
|
|
if err = options.Set(src, "time", ".TimeKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "level", ".LevelKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "message", ".MessageKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "caller", ".SourceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSlogKeys() options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
var err error
|
|
|
|
if err = options.Set(src, slog.TimeKey, ".TimeKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, slog.LevelKey, ".LevelKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, slog.MessageKey, ".MessageKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, slog.SourceKey, ".SourceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMicroKeys() options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
var err error
|
|
|
|
if err = options.Set(src, "timestamp", ".TimeKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "level", ".LevelKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "msg", ".MessageKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = options.Set(src, "caller", ".SourceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|