This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
246 lines (205 loc) · 5.28 KB
/
option.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package rec
import (
"fmt"
"os"
"runtime"
)
// Option is a struct that handles the `*rec.Config` used when new `*rec.Logger`.
type Option struct {
name string
f func(*Config) error
}
func funcName() (funcName string) {
// nolint: dogsled
pc, _, _, _ := runtime.Caller(1)
name := runtime.FuncForPC(pc).Name()
return name
}
// WithUseTimestampField returns `rec.Option` for setting `config.WithUseTimestampField`.
func WithUseTimestampField(use bool) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.UseTimestampField = use
return nil
},
}
}
// WithTimestampFieldKey returns `rec.Option` for setting `config.WithTimestampFieldKey`.
func WithTimestampFieldKey(key string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.TimestampFieldKey = key
return nil
},
}
}
// WithTimestampFieldFormat returns `rec.Option` for setting `config.WithTimestampFieldFormat`.
func WithTimestampFieldFormat(format string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.TimestampFieldFormat = format
return nil
},
}
}
// WithUseSeverityField returns `rec.Option` for setting `config.WithUseSeverityField`.
func WithUseSeverityField(use bool) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.UseSeverityField = use
return nil
},
}
}
// WithSeverityFieldKey returns `rec.Option` for setting `config.WithSeverityFieldKey`.
func WithSeverityFieldKey(key string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.SeverityFieldKey = key
return nil
},
}
}
// WithSeverityThreshold returns `rec.Option` for setting `config.WithSeverityThreshold`.
func WithSeverityThreshold(severity Severity) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.SeverityThreshold = severity
return nil
},
}
}
// WithUseUppercaseSeverity returns `rec.Option` for setting `config.WithUseUppercaseSeverity`.
func WithUseUppercaseSeverity(use bool) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.UseUppercaseSeverity = use
return nil
},
}
}
// WithDefaultSeverity returns `rec.Option` for setting `config.DefaultSeverity`.
func WithDefaultSeverity(severity Severity) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.DefaultSeverity = severity
return nil
},
}
}
// WithUseHostnameField returns `rec.Option` for setting `config.WithUseHostnameField`.
func WithUseHostnameField(use bool) Option {
return withUseHostnameField(use, funcName(), os.Hostname)
}
func withUseHostnameField(use bool, funcName string, osHostname func() (string, error)) Option {
return Option{
name: funcName,
f: func(config *Config) error {
hostname, err := osHostname()
if err != nil {
return fmt.Errorf("os.Hostname: %w", err)
}
config.UseHostnameField = use
config.HostnameFieldValue = hostname
return nil
},
}
}
// WithHostnameFieldKey returns `rec.Option` for setting `config.WithHostnameFieldKey`.
func WithHostnameFieldKey(key string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.HostnameFieldKey = key
return nil
},
}
}
// WithHostnameFieldValue returns `rec.Option` for setting `config.WithHostnameFieldValue`.
func WithHostnameFieldValue(value string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.HostnameFieldValue = value
return nil
},
}
}
// WithUseCallerField returns `rec.Option` for setting `config.WithUseCallerField`.
func WithUseCallerField(use bool) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.UseCallerField = use
return nil
},
}
}
// WithCallerFieldKey returns `rec.Option` for setting `config.WithCallerFieldKey`.
func WithCallerFieldKey(key string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.CallerFieldKey = key
return nil
},
}
}
// WithCallerSkip returns `rec.Option` for setting `config.WithCallerSkip`.
func WithCallerSkip(callerSkip int) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.CallerSkip += callerSkip
return nil
},
}
}
// WithUseShortCaller returns `rec.Option` for setting `config.WithUseShortCaller`.
func WithUseShortCaller(use bool) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.UseShortCaller = use
return nil
},
}
}
// WithUseMessageField returns `rec.Option` for setting `config.WithUseMessageField`.
func WithUseMessageField(use bool) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.UseMessageField = use
return nil
},
}
}
// WithMessageFieldKey returns `rec.Option` for setting `config.WithMessageFieldKey`.
func WithMessageFieldKey(key string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.MessageFieldKey = key
return nil
},
}
}
// WithLineSeparator returns `rec.Option` for setting `config.WithLineSeparator`.
func WithLineSeparator(separator string) Option {
return Option{
name: funcName(),
f: func(config *Config) error {
config.LineSeparator = separator
return nil
},
}
}