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"
|
2020-11-04 00:38:12 +03:00
|
|
|
"os"
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
)
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Options holds logger options
|
2020-02-07 05:35:46 +08:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Out holds the output writer
|
2020-02-20 23:57:59 -08:00
|
|
|
Out io.Writer
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds exernal options
|
2020-02-07 05:35:46 +08:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// Fields holds additional metadata
|
2021-08-06 02:15:57 +03:00
|
|
|
Fields []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
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2020-11-04 00:38:12 +03:00
|
|
|
options := Options{
|
2021-01-10 18:56:39 +03:00
|
|
|
Level: DefaultLevel,
|
2021-08-06 02:15:57 +03:00
|
|
|
Fields: make([]interface{}, 0, 6),
|
2020-11-04 00:38:12 +03:00
|
|
|
Out: os.Stderr,
|
2021-07-26 09:48:15 +03:00
|
|
|
CallerSkipCount: DefaultCallerSkipCount,
|
2020-11-04 00:38:12 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:57:59 -08:00
|
|
|
// WithFields set default fields for the logger
|
2023-07-29 00:40:58 +03:00
|
|
|
func WithFields(fields ...interface{}) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fields, ".Fields")
|
2020-02-20 23:57:59 -08: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-20 23:57:59 -08: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-20 23:57:59 -08: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
|
|
|
}
|
|
|
|
}
|