-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
options.go
98 lines (86 loc) · 2.13 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package log
import (
"time"
"github.com/rs/zerolog"
)
// defaultConfig has all the options disabled, except Color and TimeFormat
var defaultConfig = Config{
Level: zerolog.NoLevel,
Filter: nil,
OutputJSON: false,
Color: true,
StackTrace: false,
TimeFormat: time.Kitchen,
Hooks: nil,
}
// Config defines configuration for the logger.
type Config struct {
Level zerolog.Level
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
Hooks []zerolog.Hook
}
type Option func(*Config)
// FilterOption sets the filter for the Logger.
func FilterOption(filter FilterFunc) Option {
return func(cfg *Config) {
cfg.Filter = filter
}
}
// LevelOption sets the level for the Logger.
// Messages with a lower level will be discarded.
func LevelOption(level zerolog.Level) Option {
return func(cfg *Config) {
cfg.Level = level
}
}
// OutputJSONOption sets the output of the logger to JSON.
// By default, the logger outputs to a human-readable format.
func OutputJSONOption() Option {
return func(cfg *Config) {
cfg.OutputJSON = true
}
}
// ColorOption add option to enable/disable coloring
// of the logs when console writer is in use
func ColorOption(val bool) Option {
return func(cfg *Config) {
cfg.Color = val
}
}
// TimeFormatOption configures timestamp format of the logger
// timestamps disabled if empty.
// it is responsibility of the caller to provider correct values
// Supported formats:
// - time.Layout
// - time.ANSIC
// - time.UnixDate
// - time.RubyDate
// - time.RFC822
// - time.RFC822Z
// - time.RFC850
// - time.RFC1123
// - time.RFC1123Z
// - time.RFC3339
// - time.RFC3339Nano
// - time.Kitchen
func TimeFormatOption(format string) Option {
return func(cfg *Config) {
cfg.TimeFormat = format
}
}
// TraceOption add option to enable/disable print of stacktrace on error log
func TraceOption(val bool) Option {
return func(cfg *Config) {
cfg.StackTrace = val
}
}
// HooksOption append hooks to the Logger hooks
func HooksOption(hooks ...zerolog.Hook) Option {
return func(cfg *Config) {
cfg.Hooks = append(cfg.Hooks, hooks...)
}
}