-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
310 lines (262 loc) · 6.38 KB
/
examples_test.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package say_test
import (
"log"
"os"
"regexp"
"runtime"
"gopkg.in/say.v0"
)
// A trick to allow examples to correctly capture stdout.
var ew = exampleWriter{w: &os.Stdout}
type exampleWriter struct {
w **os.File
}
var findDuration = regexp.MustCompile(`\d+ms`)
func (w exampleWriter) Write(p []byte) (int, error) {
// Replace durations.
p = findDuration.ReplaceAll(p, []byte("17ms"))
if _, err := (*w.w).Write(p); err != nil {
return 0, err
}
return len(p), nil
}
func init() {
say.Redirect(ew)
}
func Example() {
// Capture panics as FATAL.
defer say.CapturePanic()
say.Info("Getting list of users...")
say.Value("user_found", 42)
// Output:
// INFO Getting list of users...
// VALUE user_found:42
}
func ExampleNewLogger() {
say.SetData("weather", "sunny")
log := say.NewLogger()
log.Info("hello") // INFO hello | weather="sunny"
}
func ExampleLogger_NewLogger() {
log := new(say.Logger) // Create a clean Logger.
log.SetData("id", 5)
log2 := log.NewLogger() // log2 inherits its parent settings.
log2.AddData("age", 53)
log2.Info("hello")
// Output:
// INFO hello | id=5 age=53
}
func ExampleLogger_SetData() {
log := new(say.Logger)
log.SetData("id", 5, "foo", "bar")
log.Info("hello")
// Output:
// INFO hello | id=5 foo="bar"
}
func ExampleLogger_AddData() {
log := new(say.Logger)
log.AddData("id", 5)
log.Info("hello")
log.AddData("foo", "bar")
log.Info("dear")
// Output:
// INFO hello | id=5
// INFO dear | id=5 foo="bar"
}
func ExampleLogger_SkipStackFrames() {
log := say.NewLogger(say.SkipStackFrames(-1)) // Disable stack traces.
log.Error("Oops")
// Output:
// ERROR Oops
}
func ExampleLogger_DisableStackTraces() {
say.DisableStackTraces(true) // Disable stack traces.
say.Error("Oops")
// Output:
// ERROR Oops
}
func ExampleLogger_Event() {
log := new(say.Logger)
log.Event("new_user", "id", 7654)
// Output:
// EVENT new_user | id=7654
}
func ExampleEvent() {
say.Event("new_user", "id", 7654)
// Output:
// EVENT new_user | id=7654
}
func ExampleLogger_Events() {
log := new(say.Logger)
log.Events("file_uploaded", 3)
// Output:
// EVENT file_uploaded:3
}
func ExampleEvents() {
say.Events("file_uploaded", 3)
// Output:
// EVENT file_uploaded:3
}
func ExampleLogger_Value() {
log := new(say.Logger)
log.Value("search_items", 117)
// Output:
// VALUE search_items:117
}
func ExampleValue() {
say.Value("search_items", 117)
// Output:
// VALUE search_items:117
}
func ExampleTiming_Say() {
t := say.NewTiming()
// Do some stuff.
t.Say("duration")
// Output:
// VALUE duration:17ms
}
func ExampleTime() {
say.Time("duration", func() {
// The code that needs to be timed.
})
// Output:
// VALUE duration:17ms
}
func ExampleLogger_Time() {
log := new(say.Logger)
log.Time("duration", func() {
// The code that needs to be timed.
})
// Output:
// VALUE duration:17ms
}
func ExampleLogger_Gauge() {
log := new(say.Logger)
log.Gauge("connected_users", 73)
// Output:
// GAUGE connected_users:73
}
func ExampleGauge() {
say.Gauge("connected_users", 73)
// Output:
// GAUGE connected_users:73
}
func ExampleLogger_Debug() {
log := new(say.Logger)
say.SetDebug(false)
log.Debug("foo")
say.SetDebug(true)
log.Debug("bar")
// Output:
// DEBUG bar
}
func ExampleDebug() {
say.SetDebug(false)
say.Debug("foo")
say.SetDebug(true)
say.Debug("bar")
// Output:
// DEBUG bar
}
func ExampleLogger_Info() {
log := new(say.Logger)
log.Info("Connecting to server...", "ip", "127.0.0.1")
// Output:
// INFO Connecting to server... | ip="127.0.0.1"
}
func ExampleInfo() {
say.Info("Connecting to server...", "ip", "127.0.0.1")
// Output:
// INFO Connecting to server... | ip="127.0.0.1"
}
func ExampleLogger_Warning() {
log := new(say.Logger)
log.Warning("Could not connect to host", "host", "example.com")
// Output:
// WARN Could not connect to host | host="example.com"
}
func ExampleWarning() {
say.Warning("Could not connect to host", "host", "example.com")
// Output:
// WARN Could not connect to host | host="example.com"
}
func ExampleLogger_Error() {
log := new(say.Logger)
_, err := os.Open("foo.txt")
if err != nil {
log.Error(err) // Print an error with the stack trace.
}
}
func ExampleError() {
_, err := os.Open("foo.txt")
if err != nil {
say.Error(err) // Print an error with the stack trace.
}
}
func ExampleLogger_CheckError() {
f, err := os.Open("foo.txt")
log := new(say.Logger)
log.CheckError(err) // Print an error only if err is not nil.
defer log.CheckError(f.Close) // Call Close and print the error if not nil.
}
func ExampleCheckError() {
f, err := os.Open("foo.txt")
say.CheckError(err) // Print an error only if err is not nil.
defer say.CheckError(f.Close) // Call Close and print the error if not nil.
}
func ExampleLogger_Fatal() {
log := new(say.Logger)
_, err := os.Open("foo.txt")
if err != nil {
log.Fatal(err) // Print a fatal error with the stack trace.
}
}
func ExampleFatal() {
_, err := os.Open("foo.txt")
if err != nil {
say.Fatal(err) // Print a fatal error with the stack trace.
}
}
func ExampleLogger_CapturePanic() {
log := new(say.Logger)
defer log.CapturePanic()
panic("oops!") // The panic message will be printed with a FATAL severity.
}
func ExampleCapturePanic() {
defer say.CapturePanic()
panic("oops!") // The panic message will be printed with a FATAL severity.
}
func ExampleLogger_CaptureStandardLog() {
l := new(say.Logger)
l.CaptureStandardLog()
log.Print("Hello from the standard library!")
// Output:
// INFO Hello from the standard library!
}
func ExampleCaptureStandardLog() {
say.CaptureStandardLog()
log.Print("Hello from the standard library!")
// Output:
// INFO Hello from the standard library!
}
func ExampleHook() {
goroutinesHook := say.Hook(func() interface{} {
return runtime.NumGoroutine
})
// Print the current number of goroutines with each message.
say.SetData("num_goroutine", goroutinesHook)
}
func ExampleDebugHook() {
query := "SELECT * FROM users WHERE id = ?"
say.SetDebug(true)
say.Event("db.get_user", "query", say.DebugHook(query)) // Print the query.
say.SetDebug(false)
say.Event("db.get_user", "query", say.DebugHook(query)) // Omit the query.
// Output:
// EVENT db.get_user | query="SELECT * FROM users WHERE id = ?"
// EVENT db.get_user
}
func ExampleTimeHook() {
// Print the current timestamp with each message.
say.SetData("num_goroutine", say.TimeHook("2006-01-02 15:04:05"))
}