-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopts.go
77 lines (65 loc) · 2.01 KB
/
opts.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
package logger
import (
"io"
"os"
)
// LoggerOption defines an applicator interface
type LoggerOption interface { //nolint:all
Apply(l *Logger)
}
// LoggerOptionFunc defines a function which modifies a logger
type LoggerOptionFunc func(l *Logger) //nolint:all
// Apply redirects a function call to the function receiver
func (lof LoggerOptionFunc) Apply(l *Logger) {
lof(l)
}
// WithNowFunc overrides default function used to determine current time.
// Intended to be used in tests only.
func WithNowFunc(nowFunc NowFunc) LoggerOption {
return LoggerOptionFunc(func(l *Logger) {
l.now = nowFunc
})
}
// WithOutput overrides default output the logs are written to.
func WithOutput(output io.Writer) LoggerOption {
return LoggerOptionFunc(func(l *Logger) {
l.output = output
})
}
// WithLevel sets minimum level for filtering logs
func WithLevel(level Level) LoggerOption {
return LoggerOptionFunc(func(l *Logger) {
l.level = level
})
}
// WithLevelName sets minimum level for filtering logs by name
func WithLevelName(level string) LoggerOption {
return LoggerOptionFunc(func(l *Logger) {
lvl, ok := LevelNameToLevel(level)
if !ok {
lvl = LevelWarn
l.Warn("Invalid log level, defaulting to Warn")
}
l.level = lvl
})
}
// WithReportCaller allows enabling/disabling including calling method in the log entry
func WithReportCaller(enable bool) LoggerOption {
return LoggerOptionFunc(func(l *Logger) {
l.reportCaller = enable
})
}
// WithHookFunc allows for connecting a hook to the logger, which will be triggered on all log-entries.
func WithHookFunc(hook HookFunc) LoggerOption {
return WithHook(hook)
}
// WithHook allows for connecting a hook to the logger, which will be triggered on all log-entries.
func WithHook(hook Hook) LoggerOption {
return LoggerOptionFunc(func(l *Logger) {
l.logrusLogger.Hooks.Add(&logrusHook{hook: hook})
})
}
// WithLevelFromEnv allows to Configure Logger from Environment Variable.
func WithLevelFromEnv(envName string) LoggerOption {
return WithLevelName(os.Getenv(envName))
}