-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_level.go
69 lines (55 loc) · 1.51 KB
/
option_level.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
package log
import "os"
type withLevel Level
func (w withLevel) applySyslog(l *syslogLogger) error {
l.level = Level(w)
return nil
}
func (w withLevel) applyStdLog(l *stdLogger) error {
l.level = Level(w)
return nil
}
// WithLevel specifies the level for loggers.
func WithLevel(level Level) Option {
return withLevel(level)
}
// WithLevelString specifies the level for loggers as a string.
func WithLevelString(str string) OptionLoader {
return func() (Option, error) {
level, err := levelFromString(str)
if err != nil {
return nil, newConfigError(err)
}
return withLevel(level), nil
}
}
// WithLevelFromEnv makes a WithLevel option based on the specified environment variable env or defaultLevel if no
// environment variable was found.
func WithLevelFromEnv(env string, defaultLevel Level) OptionLoader {
return func() (Option, error) {
level := defaultLevel
if value, found := os.LookupEnv(env); found {
var err error
level, err = levelFromString(value)
if err != nil {
return nil, newEnvironmentConfigError(env, err)
}
}
return withLevel(level), nil
}
}
type withPrintLevel Level
func (w withPrintLevel) applySyslog(l *syslogLogger) error {
l.printLevel = Level(w)
return nil
}
func (w withPrintLevel) applyStdLog(l *stdLogger) error {
l.printLevel = Level(w)
return nil
}
// WithPrintLevel specifies the level for log.Print and log.Printf.
func WithPrintLevel(level Level) Option {
return withPrintLevel(level)
}
var _ Option = withLevel(Debug)
var _ Option = withPrintLevel(Debug)