2020-02-06 23:35:46 +02:00
|
|
|
package logger
|
|
|
|
|
2020-02-06 23:39:08 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2020-02-21 09:57:59 +02:00
|
|
|
"io"
|
2020-02-06 23:39:08 +02:00
|
|
|
)
|
2020-02-06 23:35:46 +02:00
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
type Options struct {
|
2020-02-21 09:57:59 +02:00
|
|
|
// It's common to set this to a file, or leave it default which is `os.Stderr`
|
|
|
|
Out io.Writer
|
|
|
|
// Alternative options
|
2020-02-06 23:35:46 +02:00
|
|
|
Context context.Context
|
2023-04-26 02:16:34 +02:00
|
|
|
// fields to always be logged
|
|
|
|
Fields map[string]interface{}
|
|
|
|
// Caller skip frame count for file:line info
|
|
|
|
CallerSkipCount int
|
|
|
|
// The logging level the logger should log at. default is `InfoLevel`
|
|
|
|
Level Level
|
2020-02-06 23:35:46 +02:00
|
|
|
}
|
2020-02-20 10:26:12 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithFields set default fields for the logger.
|
2020-02-21 09:57:59 +02:00
|
|
|
func WithFields(fields map[string]interface{}) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.Fields = fields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithLevel set default level for the logger.
|
2020-02-21 09:57:59 +02:00
|
|
|
func WithLevel(level Level) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.Level = level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithOutput set default output writer for the logger.
|
2020-02-21 09:57:59 +02:00
|
|
|
func WithOutput(out io.Writer) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.Out = out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithCallerSkipCount set frame count to skip.
|
2020-04-26 16:11:53 +02:00
|
|
|
func WithCallerSkipCount(c int) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.CallerSkipCount = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:57:59 +02:00
|
|
|
func SetOption(k, v interface{}) Option {
|
2020-02-20 10:26:12 +02:00
|
|
|
return func(o *Options) {
|
2020-02-21 09:57:59 +02:00
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, k, v)
|
2020-02-20 10:26:12 +02:00
|
|
|
}
|
|
|
|
}
|