/
examples_test.go
182 lines (149 loc) · 5.76 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
package logger_test
import (
"fmt"
"log"
"regexp"
"time"
"github.com/eltorocorp/nobslogger/logger"
)
type fakeWriter struct{}
// Write just replaces the timestamp internally assigned by the LogService
// with a constant value so the tests remain deterministic.
func (fakeWriter) Write(message []byte) (int, error) {
re := regexp.MustCompile(`\d{19}`)
msg := re.ReplaceAllString(string(message), "1234567890123456789")
fmt.Println(msg)
return len(msg), nil
}
func ExampleInitializeWriter() {
// Establish a ServiceContext.
// This records the highest level information about the system being logged.
serviceContext := logger.ServiceContext{
Environment: "test",
SystemName: "examples",
ServiceName: "example runner",
ServiceInstanceID: "1",
}
// Initialize the LogService.
loggerSvc := logger.InitializeWriter(new(fakeWriter), serviceContext)
// Get a new logger (LogContext) from the LogService.
logger := loggerSvc.NewContext("ExampleInitializeWriter_ServiceContext", "running example")
// Log something.
logger.Info("Here is some info")
// Calling Cancel signals to the LogService to begin flushing the internal
// log queue.
loggerSvc.Cancel()
// Wait always blocks while the LogService is active, and will only unblock
// after the Cancel method has been called and has finished flusing the
// log message queue.
loggerSvc.Wait()
// Output: {"timestamp":"1234567890123456789","environment":"test","system_name":"examples","service_name":"example runner","service_instance_id":"1","site":"ExampleInitializeWriter_ServiceContext","operation":"running example","level":"300","severity":"info","msg":"Here is some info","details":""}
}
// LogService supports having multiple logging contexts that may be initialized
// from separate goroutines.
func ExampleLogService_multipleContexts() {
serviceContext := logger.ServiceContext{
Environment: "test",
SystemName: "examples",
ServiceName: "example runner",
ServiceInstanceID: "1",
}
loggerSvc := logger.InitializeWriter(new(fakeWriter), serviceContext)
go func() {
logger := loggerSvc.NewContext("goroutine 1", "running example")
logger.Info("Here is some info from goroutine 1")
}()
go func() {
logger := loggerSvc.NewContext("goroutine 2", "running example")
logger.Info("Here is some info from goroutine 2")
}()
loggerSvc.Cancel()
loggerSvc.Wait()
}
// LogService supports having one (or more) log contexts span multiple
// goroutines.
func ExampleLogService_contextAcrossGoroutines() {
serviceContext := logger.ServiceContext{
Environment: "test",
SystemName: "examples",
ServiceName: "example runner",
ServiceInstanceID: "1",
}
loggerSvc := logger.InitializeWriter(new(fakeWriter), serviceContext)
logger := loggerSvc.NewContext("single context", "used across multiple goroutines")
logger.Info("Log from goroutine 1")
go func() {
logger.Info("Log from goroutine 2")
}()
go func() {
logger.Info("Log from goroutine 3")
}()
loggerSvc.Cancel()
loggerSvc.Wait()
}
// LogService supports cancellation from a separate goroutine from where the
// service was originally initialized.
func ExampleLogService_cancelFromSeparateGoroutine() {
serviceContext := logger.ServiceContext{
Environment: "test",
SystemName: "examples",
ServiceName: "example runner",
ServiceInstanceID: "1",
}
loggerSvc := logger.InitializeWriter(new(fakeWriter), serviceContext)
logger := loggerSvc.NewContext("single context", "used across multiple goroutines")
go func() {
for {
logger.Warn("infinite loop")
time.Sleep(1 * time.Second)
}
}()
go func() {
logger.Info("Cancelling")
loggerSvc.Cancel()
}()
loggerSvc.Wait()
}
// LogService contexts support a vartiety of log methods, including, but not
// limited to those shown in this example.
func ExampleLogService_variousContextMethods() {
serviceContext := logger.ServiceContext{
Environment: "test",
SystemName: "examples",
ServiceName: "example runner",
ServiceInstanceID: "1",
}
loggerSvc := logger.InitializeWriter(new(fakeWriter), serviceContext)
logger := loggerSvc.NewContext("goroutine 1", "running example")
logger.Info("An info-level message.")
logger.InfoD("An info-level message.", "With more details!")
logger.Debug("A debug-level message")
logger.DebugD("A debug-level message.", "With extra details!")
loggerSvc.Cancel()
loggerSvc.Wait()
}
// LogContexts also support the io.Writer interface, so they can be used to
// hook into any external writer, such as through the use of `log.SetOutput`.
func ExampleLogContext_Write() {
serviceContext := logger.ServiceContext{
Environment: "test",
SystemName: "examples",
ServiceName: "example runner",
ServiceInstanceID: "1",
}
loggerSvc := logger.InitializeWriter(new(fakeWriter), serviceContext)
logger := loggerSvc.NewContext("ExampleLogContext", "Write")
// Here we simulate hooking the LogContext into the an existing std/logger.
// In this example we create a new logger via `log.New`, the this could also
// be done using `log.SetOutput`.
stdlibLogger := log.New(logger, "", 0)
// When we call Println, the current LogContext will log the message at the
// Trace level.
// Note that the expected output will include an escaped newline character.
// This is added by the Println function, and is properly escaped by
// nobslogger to prevent mangling the JSON output.
stdlibLogger.Println("Hello from the standard library logger!")
loggerSvc.Cancel()
loggerSvc.Wait()
// Output: {"timestamp":"1234567890123456789","environment":"test","system_name":"examples","service_name":"example runner","service_instance_id":"1","site":"ExampleLogContext","operation":"Write","level":"100","severity":"trace","msg":"Hello from the standard library logger!\n","details":""}
}