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"
|
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
|
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
|
|
|
}
|
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
|
|
|
|
func WithContextAttrFuncs(attrs ...interface{}) 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) {
|
|
|
|
v = reflect.MakeSlice(reflect.TypeOf(v), 0, len(attrs)).Interface()
|
|
|
|
}
|
|
|
|
cv := reflect.ValueOf(v)
|
|
|
|
for _, l := range attrs {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|