-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_location.go
93 lines (75 loc) · 2.35 KB
/
option_location.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
package log
import (
"fmt"
stdlog "log"
"os"
)
func sourceLocationFormatFromString(str string) (int, error) {
switch str {
case "DISABLED", "Disabled", "disabled":
return 0, nil
case "SHORT", "Short", "short":
return stdlog.Lshortfile, nil
case "LONG", "Long", "long":
return stdlog.Lshortfile, nil
}
return 0, fmt.Errorf("unknown source-location format '%s'", str)
}
type withSourceLocation int
func (w withSourceLocation) applySyslog(l *syslogLogger) error {
if w == 0 {
l.flags.enable(stdlog.Lshortfile|stdlog.Llongfile, false)
} else {
l.flags.enable(int(w), true)
}
return nil
}
func (w withSourceLocation) applyStdLog(l *stdLogger) error {
if w == 0 {
l.flags.enable(stdlog.Lshortfile|stdlog.Llongfile, false)
} else {
l.flags.enable(int(w), true)
}
return nil
}
// WithSourceLocationDisabled disables caller-location in log-lines.
func WithSourceLocationDisabled() Option {
return withSourceLocation(0)
}
// WithSourceLocationShort specifies the caller-location in log-lines to have short filename.
func WithSourceLocationShort() Option {
return withSourceLocation(stdlog.Lshortfile)
}
// WithSourceLocationLong specifies the caller-location in log-lines to have long filename.
func WithSourceLocationLong() Option {
return withSourceLocation(stdlog.Llongfile)
}
// WithSourceLocation specifies the caller-location format as a string; allowed values are "short", "long", "disabled".
func WithSourceLocation(value string) OptionLoader {
return func() (Option, error) {
format, err := sourceLocationFormatFromString(value)
if err != nil {
return nil, newConfigError(err)
}
return withSourceLocation(format), nil
}
}
// WithSourceLocationFromEnv sets the caller-location option based on either the specified environment variable env or
// the defaultFormat if no environment variable is found.
func WithSourceLocationFromEnv(env string, defaultFormat string) OptionLoader {
return func() (Option, error) {
if value, found := os.LookupEnv(env); found {
format, err := sourceLocationFormatFromString(value)
if err != nil {
return nil, newEnvironmentConfigError(env, err)
}
return withSourceLocation(format), nil
}
format, err := sourceLocationFormatFromString(defaultFormat)
if err != nil {
return nil, newConfigError(err)
}
return withSourceLocation(format), nil
}
}
var _ Option = withSourceLocation(0)