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"
|
2024-03-07 00:08:34 +03:00
|
|
|
"time"
|
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
|
2024-03-09 23:35:13 +03:00
|
|
|
// TimeFunc used to obtain current time
|
|
|
|
TimeFunc func() time.Time
|
2023-10-17 01:00:00 +03:00
|
|
|
// TimeKey is the key used for the time of the log call
|
|
|
|
TimeKey string
|
2024-03-09 23:35:13 +03:00
|
|
|
// Name holds the logger name
|
|
|
|
Name string
|
2023-10-17 01:00:00 +03:00
|
|
|
// 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
|
2024-03-05 01:27:22 +03:00
|
|
|
// ErrorKey is the key used for the error info
|
|
|
|
ErrorKey string
|
2023-10-17 01:00:00 +03:00
|
|
|
// SourceKey is the key used for the source file and line of the log call
|
|
|
|
SourceKey string
|
2024-03-04 23:42:15 +03:00
|
|
|
// StacktraceKey is the key used for the stacktrace
|
|
|
|
StacktraceKey string
|
2024-03-09 23:35:13 +03:00
|
|
|
// Attrs holds additional attributes
|
|
|
|
Attrs []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
|
2024-03-06 01:17:19 +03:00
|
|
|
// AddStacktrace controls writing of stacktaces on error
|
|
|
|
AddStacktrace bool
|
|
|
|
// AddSource enabled writing source file and position in log
|
|
|
|
AddSource bool
|
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,
|
2024-03-06 01:17:19 +03:00
|
|
|
AddSource: true,
|
2024-03-07 00:08:34 +03:00
|
|
|
TimeFunc: time.Now,
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
|
2024-03-05 01:27:22 +03:00
|
|
|
_ = WithMicroKeys()(&options)
|
2023-10-17 01:00:00 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
2024-03-05 01:27:22 +03:00
|
|
|
_ = o(&options)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2024-03-04 23:42:15 +03:00
|
|
|
if err = options.Set(src, "stacktrace", ".StacktraceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-05 01:27:22 +03:00
|
|
|
if err = options.Set(src, "error", ".ErrorKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
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
|
|
|
|
}
|
2024-03-04 23:42:15 +03:00
|
|
|
if err = options.Set(src, "stacktrace", ".StacktraceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-05 01:27:22 +03:00
|
|
|
if err = options.Set(src, "error", ".ErrorKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
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
|
|
|
|
}
|
2024-03-04 23:42:15 +03:00
|
|
|
if err = options.Set(src, "stacktrace", ".StacktraceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-05 01:27:22 +03:00
|
|
|
if err = options.Set(src, "error", ".ErrorKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
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
|
|
|
|
}
|
2024-03-04 23:42:15 +03:00
|
|
|
if err = options.Set(src, "stacktrace", ".StacktraceKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-05 01:27:22 +03:00
|
|
|
if err = options.Set(src, "error", ".ErrorKey"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 23:42:15 +03:00
|
|
|
|
2024-03-06 01:17:19 +03:00
|
|
|
// WithAddStacktrace controls writing stacktrace on error
|
|
|
|
func WithAddStacktrace(v bool) options.Option {
|
2024-03-04 23:42:15 +03:00
|
|
|
return func(src interface{}) error {
|
2024-03-06 01:17:19 +03:00
|
|
|
return options.Set(src, v, ".AddStacktrace")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WitAddSource controls writing source file and pos in log
|
|
|
|
func WithAddSource(v bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, v, ".AddSource")
|
2024-03-04 23:42:15 +03:00
|
|
|
}
|
|
|
|
}
|