-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.go
98 lines (82 loc) · 2.09 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 lgr
import (
"io"
"strings"
)
// Option func type
type Option func(l *Logger)
// Out sets output writer, stdout by default
func Out(w io.Writer) Option {
return func(l *Logger) {
l.stdout = w
}
}
// Err sets error writer, stderr by default
func Err(w io.Writer) Option {
return func(l *Logger) {
l.stderr = w
}
}
// Debug turn on dbg mode
func Debug(l *Logger) {
l.dbg = true
}
// Trace turn on trace + dbg mode
func Trace(l *Logger) {
l.dbg = true
l.trace = true
}
// CallerDepth sets number of stack frame skipped for caller reporting, 0 by default
func CallerDepth(n int) Option {
return func(l *Logger) {
l.callerDepth = n
}
}
// Format sets output layout, overwrites all options for individual parts, i.e. Caller*, Msec and LevelBraces
func Format(f string) Option {
return func(l *Logger) {
l.format = f
}
}
// CallerFunc adds caller info with function name. Ignored if Format option used.
func CallerFunc(l *Logger) {
l.callerFunc = true
}
// CallerPkg adds caller's package name. Ignored if Format option used.
func CallerPkg(l *Logger) {
l.callerPkg = true
}
// LevelBraces surrounds level with [], i.e. [INFO]. Ignored if Format option used.
func LevelBraces(l *Logger) {
l.levelBraces = true
}
// CallerFile adds caller info with file, and line number. Ignored if Format option used.
func CallerFile(l *Logger) {
l.callerFile = true
}
// Msec adds .msec to timestamp. Ignored if Format option used.
func Msec(l *Logger) {
l.msec = true
}
// Secret sets list of substring to be hidden, i.e. replaced by "******"
// Useful to prevent passwords or other sensitive tokens to be logged.
func Secret(vals ...string) Option {
return func(l *Logger) {
for _, v := range vals {
if strings.TrimSpace(v) == "" {
continue // skip empty secrets
}
l.secrets = append(l.secrets, []byte(v))
}
}
}
// Map sets mapper functions to change elements of the logged message based on levels.
func Map(m Mapper) Option {
return func(l *Logger) {
l.mapper = m
}
}
// StackTraceOnError turns on stack trace for ERROR level.
func StackTraceOnError(l *Logger) {
l.errorDump = true
}