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
|
|
|
// The logging level the logger should log at. default is `InfoLevel`
|
2020-02-20 10:26:12 +02:00
|
|
|
Level Level
|
2020-02-21 09:57:59 +02:00
|
|
|
// fields to always be logged
|
|
|
|
Fields map[string]interface{}
|
|
|
|
// It's common to set this to a file, or leave it default which is `os.Stderr`
|
|
|
|
Out io.Writer
|
2020-04-26 16:11:53 +02:00
|
|
|
// Caller skip frame count for file:line info
|
|
|
|
CallerSkipCount int
|
2020-02-21 09:57:59 +02:00
|
|
|
// Alternative options
|
2020-02-06 23:35:46 +02:00
|
|
|
Context context.Context
|
|
|
|
}
|
2020-02-20 10:26:12 +02:00
|
|
|
|
2020-02-21 09:57:59 +02:00
|
|
|
// WithFields set default fields for the logger
|
|
|
|
func WithFields(fields map[string]interface{}) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.Fields = fields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel set default level for the logger
|
|
|
|
func WithLevel(level Level) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.Level = level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOutput set default output writer for the logger
|
|
|
|
func WithOutput(out io.Writer) Option {
|
|
|
|
return func(args *Options) {
|
|
|
|
args.Out = out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 16:11:53 +02:00
|
|
|
// WithCallerSkipCount set frame count to skip
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|