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"
|
2020-11-04 00:38:12 +03:00
|
|
|
"os"
|
2020-02-07 00:39:08 +03:00
|
|
|
)
|
2020-02-07 00:35:46 +03:00
|
|
|
|
2024-03-04 23:03:55 +03:00
|
|
|
// Option func signature
|
2020-02-07 00:35:46 +03:00
|
|
|
type Option func(*Options)
|
|
|
|
|
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
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name holds the logger name
|
|
|
|
Name string
|
2024-03-04 23:03:55 +03:00
|
|
|
// Fields holds additional metadata
|
|
|
|
Fields []interface{}
|
2021-09-30 20:32:59 +03:00
|
|
|
// CallerSkipCount number of frmaes to skip
|
|
|
|
CallerSkipCount int
|
2024-03-04 23:03:55 +03:00
|
|
|
// Stacktrace controls writing of stacktaces on error
|
|
|
|
Stacktrace bool
|
|
|
|
// The logging level the logger should log
|
|
|
|
Level Level
|
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
|
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,
|
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-21 10:57:59 +03:00
|
|
|
// WithFields set default fields for the logger
|
2021-08-06 02:15:57 +03:00
|
|
|
func WithFields(fields ...interface{}) Option {
|
2021-01-10 18:56:39 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Fields = fields
|
2020-02-21 10:57:59 +03: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-21 10:57:59 +03: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-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 23:03:55 +03:00
|
|
|
// WithStacktrace controls writing stacktrace on error
|
|
|
|
func WithStacktrace(v bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Stacktrace = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-04-27 08:32:47 +03:00
|
|
|
func WithName(n string) Option {
|
2021-01-29 14:07:35 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|