-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
259 lines (192 loc) · 6.65 KB
/
example_test.go
File metadata and controls
259 lines (192 loc) · 6.65 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
package slogger_test
import (
"context"
"errors"
"flag"
"github.com/codenaugh/slogger"
)
func expensiveFunc() string {
// expensive operation
return "expensive data"
}
func ExampleEnableCloudOutput() {
var local, debug bool
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
flag.BoolVar(&local, "local", false, "Running locally; Pretty Text instead of JSON log messages")
flag.Parse()
if !local {
slogger.EnableCloudOutput()
}
if debug {
slogger.EnableDebugOutput()
}
slogger.Info("this will output a JSON-formatted log message")
}
func ExampleEnableDebugOutput() {
var local, debug bool
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
flag.BoolVar(&local, "local", false, "Running locally; Pretty Text instead of JSON log messages")
flag.Parse()
if !local {
slogger.EnableCloudOutput()
}
if debug {
slogger.EnableDebugOutput()
}
slogger.Debug("this will output if debug is enabled, or be suppressed if not")
}
func ExampleIsDebugOutputEnabled() {
if slogger.IsDebugOutputEnabled() {
slogger.Debugf("let's output some expensive data: %v", expensiveFunc())
}
}
func ExampleDisableColoredOutput() {
slogger.DisableColoredOutput()
slogger.Info("this will output without any added color")
}
func ExampleDisableFileOutput() {
slogger.DisableFileOutput()
slogger.Info("this will output without any file info")
// Output
// 11-20-2024 16:12:02.556 INFO this will output without any file info
}
func ExampleDebug() {
slogger.EnableDebugOutput()
argsString := "args"
slogger.Debug("debug message")
slogger.Debug("debug message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: DEBUG debug message
// 11-20-2024 16:12:02.556 main.go:17: DEBUG debug message with extra args 10
}
func ExampleInfo() {
argsString := "args"
slogger.Info("info message")
slogger.Info("info message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: INFO info message
// 11-20-2024 16:12:02.556 main.go:17: INFO message with extra args 10
}
func ExampleWarn() {
argsString := "args"
slogger.Warn("warning message")
slogger.Warn("warning message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: WARN warning message
// 11-20-2024 16:12:02.556 main.go:17: WARN warning message with extra args 10
}
func ExampleError() {
argsString := "args"
err := errors.New("error message")
slogger.Error(err)
slogger.Error("error message")
slogger.Error("error message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message with extra args 10
}
func ExampleFatal() {
err := errors.New("fatal error")
slogger.Fatal(err)
// Output
// 11-20-2024 16:12:02.556 main.go:17: ERROR fatal error
// exit status 1
slogger.Fatal("fatal message:", err)
// Output
// 11-20-2024 16:12:02.556 main.go:17: ERROR fatal message: fatal error
// exit status 1
}
func ExampleDebugf() {
slogger.EnableDebugOutput()
argsString := "args"
slogger.Debugf("debug message")
slogger.Debugf("debug message %s %s %v: %d", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: DEBUG debug message
// 11-20-2024 16:12:02.556 main.go:17: DEBUG debug message with extra args: 10
}
func ExampleInfof() {
argsString := "args"
slogger.Infof("info message")
slogger.Infof("info message %s %s %v: %d", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: INFO info message
// 11-20-2024 16:12:02.556 main.go:17: INFO info message with extra args: 10
}
func ExampleWarnf() {
argsString := "args"
slogger.Warnf("warning message")
slogger.Warnf("warning message %s %s %v: %d", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: WARN warning message
// 11-20-2024 16:12:02.556 main.go:17: WARN warning message with extra args: 10
}
func ExampleErrorf() {
argsString := "args"
slogger.Errorf("error message")
slogger.Errorf("error message %s %s %v: %d", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message with extra args: 10
}
func ExampleFatalf() {
err := errors.New("fatal error")
slogger.Fatalf("slogger fatal message: %v", err)
// Output
// 11-20-2024 16:12:02.556 main.go:17: ERROR slogger fatal message: fatal error
// exit status 1
}
func ExampleDebugContext() {
slogger.EnableDebugOutput()
ctx := context.Background()
argsString := "args"
slogger.DebugContext(ctx, "debug message")
slogger.DebugContext(ctx, "debug message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: DEBUG debug message
// 11-20-2024 16:12:02.556 main.go:17: DEBUG debug message with extra args 10
}
func ExampleInfoContext() {
ctx := context.Background()
argsString := "args"
slogger.InfoContext(ctx, "info message")
slogger.InfoContext(ctx, "info message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: INFO info message
// 11-20-2024 16:12:02.556 main.go:17: INFO info message with extra args 10
}
func ExampleWarnContext() {
ctx := context.Background()
argsString := "args"
slogger.WarnContext(ctx, "warning message")
slogger.WarnContext(ctx, "warning message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: WARN warning message
// 11-20-2024 16:12:02.556 main.go:17: WARN warning message with extra args 10
}
func ExampleErrorContext() {
ctx := context.Background()
argsString := "args"
slogger.ErrorContext(ctx, "error message")
slogger.ErrorContext(ctx, "error message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message
// 11-20-2024 16:12:02.556 main.go:17: ERROR error message with extra args 10
}
func ExampleLog() {
argsString := "args"
slogger.Log(context.Background(), slogger.LevelInfo, "slogger log message")
slogger.Log(context.Background(), slogger.LevelInfo, "slogger log message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: INFO slogger log message
// 11-20-2024 16:12:02.556 main.go:17: INFO slogger log message with extra args 10
}
func ExampleOutput() {
argsString := "args"
slogger.Output(2, slogger.LevelInfo, "slogger output message")
slogger.Output(2, slogger.LevelInfo, "slogger output message", "with", "extra", argsString, 10)
// Output
// 11-20-2024 16:12:02.556 main.go:17: INFO slogger output message
// 11-20-2024 16:12:02.556 main.go:17: INFO slogger output message with extra args 10
}