-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
177 lines (157 loc) · 4.5 KB
/
options.go
File metadata and controls
177 lines (157 loc) · 4.5 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package log
import (
"context"
"fmt"
"io"
"os"
)
// Option represents a function that does one or more alterations to 'logOptions' inside the logger.
type Option func(*logOptions)
// logOptions contains all the configuration options for the logger.
type logOptions struct {
prefix string
suffix string
colors bool
logLevel Level
filePath bool
funcPath bool
skipFrameCount int
writer io.Writer
output Output
ctxExt func(ctx context.Context) []interface{}
ctxMapExt func(ctx context.Context) map[string]string
ctxTraceExt func(ctx context.Context) string
}
// applyDefault applies a set of predefined configurations to the logger.
func (lOpts *logOptions) applyDefault() {
lOpts.skipFrameCount = 2
lOpts.colors = true
lOpts.logLevel = TRACE
lOpts.filePath = false
lOpts.funcPath = false
lOpts.writer = os.Stdout
lOpts.output = OutText
}
// copy returns a copy of existing configuration values of the logger.
func (lOpts *logOptions) copy() *logOptions {
return &logOptions{
prefix: lOpts.prefix,
suffix: lOpts.suffix,
colors: lOpts.colors,
logLevel: lOpts.logLevel,
filePath: lOpts.filePath,
funcPath: lOpts.funcPath,
skipFrameCount: lOpts.skipFrameCount,
writer: lOpts.writer,
ctxExt: lOpts.ctxExt,
ctxMapExt: lOpts.ctxMapExt,
ctxTraceExt: lOpts.ctxTraceExt,
}
}
// apply applies given configuration values to the logger.
//
// This will replace existing configuration values with provided values.
func (lOpts *logOptions) apply(options ...Option) {
for _, opt := range options {
opt(lOpts)
}
}
// Deprecated: use WithSkipFrameCount instead.
//
// FileDepth sets the frame count to skip when reading filepath, func path.
func FileDepth(d int) Option {
return func(opts *logOptions) {
opts.skipFrameCount = d
}
}
// WithStdOut sets the log writer.
func WithStdOut(w io.Writer) Option {
return func(opts *logOptions) {
opts.writer = w
}
}
// WithSkipFrameCount sets the frame count to skip when reading filepath, func path.
func WithSkipFrameCount(c int) Option {
return func(opts *logOptions) {
opts.skipFrameCount = c
}
}
// WithOutput sets the output format for log entries.
func WithOutput(o Output) Option {
return func(opts *logOptions) {
opts.output = o
}
}
// WithFilePath sets whether the file path is logged or not.
func WithFilePath(enabled bool) Option {
return func(opts *logOptions) {
opts.filePath = enabled
}
}
// WithFuncPath sets whether the func path is logged or not.
func WithFuncPath(enabled bool) Option {
return func(opts *logOptions) {
opts.funcPath = enabled
}
}
// Prefixed appends the given prefix value to the existing prefix value.
func Prefixed(prefix string) Option {
return func(opts *logOptions) {
if opts.prefix != `` {
opts.prefix = fmt.Sprintf(`%s.%s`, opts.prefix, prefix)
return
}
opts.prefix = prefix
}
}
// WithColors enables colours in log messages.
func WithColors(enabled bool) Option {
return func(opts *logOptions) {
opts.colors = enabled
}
}
// WithLevel sets the log level.
//
// The log level is used to determine which types of logs are logged depending on the precedence of the log level.
func WithLevel(level Level) Option {
return func(opts *logOptions) {
opts.logLevel = level
}
}
// Deprecated: use WithCtxMapExtractor instead.
//
// WithCtxExtractor allows setting up a function to extract values from the context as an array.
func WithCtxExtractor(fn func(ctx context.Context) []interface{}) Option {
return func(opts *logOptions) {
parent := opts.ctxExt
opts.ctxExt = func(ctx context.Context) []interface{} {
if parent != nil {
return append(parent(ctx), fn(ctx)...)
}
return fn(ctx)
}
}
}
// WithCtxMapExtractor allows setting up a function to extract values from the context as a key:value map.
func WithCtxMapExtractor(fn func(ctx context.Context) map[string]string) Option {
return func(opts *logOptions) {
parentExt := opts.ctxMapExt
opts.ctxMapExt = func(ctx context.Context) map[string]string {
if parentExt != nil {
parentMap := parentExt(ctx)
for key, val := range fn(ctx) {
parentMap[key] = val
}
return parentMap
}
return fn(ctx)
}
}
}
// WithCtxTraceExtractor allows setting up of a function to extract trace from the context.
// Default value func(_ context.Context) string{return ""}
func WithCtxTraceExtractor(fn func(ctx context.Context) string) Option {
return func(opts *logOptions) {
opts.ctxTraceExt = fn
}
}