-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoptions.go
More file actions
133 lines (116 loc) · 3.45 KB
/
options.go
File metadata and controls
133 lines (116 loc) · 3.45 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package lgr
import (
"io"
"log/slog"
"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.
// Note: This option only affects lgr's native text format and is ignored when using SlogHandler.
func CallerFunc(l *Logger) {
l.callerFunc = true
}
// CallerPkg adds caller's package name. Ignored if Format option used.
// Note: This option only affects lgr's native text format and is ignored when using SlogHandler.
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.
// Note: This option only affects lgr's native text format and is ignored when using SlogHandler.
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
}
// SlogHandler sets slog.Handler to delegate logging to. When using this option,
// the output format will be controlled by the slog.Handler provided, not by lgr's
// format options.
//
// IMPORTANT: When using lgr.SlogHandler:
//
// 1. To get caller information in JSON output, you must create the handler with
// slog.HandlerOptions{AddSource: true}.
//
// 2. The lgr caller info options (lgr.CallerFile, lgr.CallerFunc) do NOT affect
// JSON output from slog handlers. They only work with lgr's native text format.
//
// Example of correct setup for JSON with caller info:
//
// // create handler with AddSource enabled
// jsonHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
// AddSource: true, // This enables caller information in JSON output
// })
//
// // use handler with lgr
// logger := lgr.New(lgr.SlogHandler(jsonHandler))
//
// For text format with caller info, use lgr's native caller options:
//
// logger := lgr.New(lgr.CallerFile, lgr.CallerFunc)
func SlogHandler(h slog.Handler) Option {
return func(l *Logger) {
l.slogHandler = h
}
}