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-06 21:39:08 +00:00
|
|
|
)
|
2020-02-07 05:35:46 +08:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Option func
|
2020-02-07 05:35:46 +08:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Options holds logger options
|
2020-02-07 05:35:46 +08:00
|
|
|
type Options struct {
|
2021-01-29 14:07:35 +03:00
|
|
|
Name string
|
2020-02-20 23:57:59 -08:00
|
|
|
// The logging level the logger should log at. default is `InfoLevel`
|
2020-02-20 08:26:12 +00:00
|
|
|
Level Level
|
2020-02-20 23:57:59 -08: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 17:11:53 +03:00
|
|
|
// Caller skip frame count for file:line info
|
|
|
|
CallerSkipCount int
|
2020-02-20 23:57:59 -08:00
|
|
|
// Alternative options
|
2020-02-07 05:35:46 +08:00
|
|
|
Context context.Context
|
|
|
|
}
|
2020-02-20 08:26:12 +00:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewOptions creates new options struct
|
2020-10-16 09:38:57 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
2020-11-04 00:38:12 +03:00
|
|
|
options := Options{
|
2021-01-10 18:56:39 +03:00
|
|
|
Level: DefaultLevel,
|
2020-11-04 00:38:12 +03:00
|
|
|
Fields: make(map[string]interface{}),
|
|
|
|
Out: os.Stderr,
|
2021-01-10 18:56:39 +03:00
|
|
|
CallerSkipCount: 0,
|
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
|
|
|
|
func WithFields(fields map[string]interface{}) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Fields = fields
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel set default level for the logger
|
|
|
|
func WithLevel(level Level) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Level = level
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOutput set default output writer for the logger
|
|
|
|
func WithOutput(out io.Writer) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Out = out
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 17:11:53 +03:00
|
|
|
// WithCallerSkipCount set frame count to skip
|
|
|
|
func WithCallerSkipCount(c int) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.CallerSkipCount = c
|
2020-04-26 17:11:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:50:29 +03:00
|
|
|
// WithContext set context
|
|
|
|
func WithContext(ctx context.Context) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
2020-11-13 14:50:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// WithName sets the name
|
|
|
|
func withName(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|