-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_examples_test.go
86 lines (73 loc) · 3.36 KB
/
logger_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
// All examples disable reporing caller as file path depends on workdir
// which is different for every run environment and therefore it's impossible
// to match exact output.
package logger
import (
"errors"
"os"
"time"
)
func mockNowFunc() time.Time {
return time.Date(2020, 10, 10, 10, 10, 10, 10, time.UTC)
}
func ExampleInfo() {
logger := New(WithNowFunc(mockNowFunc), WithReportCaller(false))
logger.Warn("foobar")
logger.Info("i won't be logged because the default log level is higher than info")
logger.Error("foobar")
// Output: {"level":"warning","msg":"foobar","time":"2020-10-10T10:10:10Z"}
// {"level":"error","msg":"foobar","time":"2020-10-10T10:10:10Z"}
}
func ExampleWithLevel() {
logger := New(WithNowFunc(mockNowFunc), WithLevel(LevelInfo), WithReportCaller(false))
logger.Info("now log level is set to info or lower, I will be logged")
// Output: {"level":"info","msg":"now log level is set to info or lower, I will be logged","time":"2020-10-10T10:10:10Z"}
}
func ExampleWithLevelName() {
logger := New(WithNowFunc(mockNowFunc), WithLevelName("info"), WithReportCaller(false))
logger.Info("now log level is set to info or lower, I will be logged")
// Output: {"level":"info","msg":"now log level is set to info or lower, I will be logged","time":"2020-10-10T10:10:10Z"}
}
func ExampleWithFields() {
// Example runner replaces os.Stdout to catch output and compares it with desired output.
// Global logger instance sets default output to os.Stdin before example runner has a chance to overwrite it.
// Within this function, os.Stdin is already replaced by example runner.
// So we need to tell the global logger instance to use modified os.Stdout as its output,
// otherwise example runner will fail as logs would be written to real stdout
// and nothing would get written to example runnner's buffer.
oldOutput := globalLogger.output
oldNowFunc := globalLogger.now
defer func() {
ConfigureGlobalLogger(WithOutput(oldOutput), WithNowFunc(oldNowFunc), WithReportCaller(true))
}()
ConfigureGlobalLogger(WithOutput(os.Stdout), WithNowFunc(mockNowFunc), WithReportCaller(false))
WithFields(Fields{
"timeSpentOnConfiguration": 0,
"defaultsLoaded": true,
}).Warn("use default logger with 0 configuration")
WithField("singleField", true).Warn("example with a single field")
// Output: {"defaultsLoaded":true,"level":"warning","msg":"use default logger with 0 configuration","time":"2020-10-10T10:10:10Z","timeSpentOnConfiguration":0}
// {"level":"warning","msg":"example with a single field","singleField":true,"time":"2020-10-10T10:10:10Z"}
}
func ExampleWithError() {
logger := New(WithNowFunc(mockNowFunc), WithReportCaller(false))
err := errors.New("Test error")
logger.WithError(err).Error("Operation failed")
// Output: {"error":"Test error","level":"error","msg":"Operation failed","time":"2020-10-10T10:10:10Z"}
}
type warner interface {
Warn(args ...interface{})
}
func funcThatAcceptsInterface(warner warner) {
warner.Warn("foobar")
}
func ExampleGlobal() {
oldOutput := globalLogger.output
oldNowFunc := globalLogger.now
defer func() {
ConfigureGlobalLogger(WithOutput(oldOutput), WithNowFunc(oldNowFunc), WithReportCaller(true))
}()
ConfigureGlobalLogger(WithOutput(os.Stdout), WithNowFunc(mockNowFunc), WithReportCaller(false))
funcThatAcceptsInterface(Global())
// Output: {"level":"warning","msg":"foobar","time":"2020-10-10T10:10:10Z"}
}