generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
107 lines (98 loc) · 2.63 KB
/
example_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
package zapring_test
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"moul.io/zapring"
)
func Example_custom() {
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.TimeKey = "" // used to make this test consistent (not depending on current timestamp)
encoder := zapcore.NewJSONEncoder(encoderConfig)
level := zap.LevelEnablerFunc(func(_ zapcore.Level) bool { return true })
ring := zapring.New(uint(10 * 1024 * 1024)) // 10Mb ring
defer ring.Close()
core := ring.
SetNextCore(zapcore.NewCore(encoder, zapcore.AddSync(ioutil.Discard), level)).
SetEncoder(encoder)
logger := zap.New(
core,
zap.Development(),
zap.AddCaller(),
)
defer logger.Sync()
logger.Info("hello world!")
logger.Info("lorem ipsum")
r, w := io.Pipe()
go func() {
_, err := ring.WriteTo(w)
if err != nil && err != io.EOF {
panic(err)
}
w.Close()
}()
scanner := bufio.NewScanner(r)
lines := 0
for scanner.Scan() {
fmt.Println("--> ", scanner.Text())
lines++
if lines == 2 {
break
}
}
// Output:
// --> {"L":"INFO","C":"zapring/example_test.go:30","M":"hello world!"}
// --> {"L":"INFO","C":"zapring/example_test.go:31","M":"lorem ipsum"}
}
func Example_composite() {
cli := zap.NewExample()
cli.Info("hello cli!")
ring := zapring.New(10 * 1024 * 1024) // 10MB ring-buffer
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.TimeKey = "" // used to make this test consistent (not depending on current timestamp)
ring.SetEncoder(zapcore.NewJSONEncoder(encoderConfig))
// FIXME: ring.Info("hello ring!")
composite := zap.New(
zapcore.NewTee(cli.Core(), ring),
zap.Development(),
)
composite.Info("hello composite!")
r, w := io.Pipe()
go func() {
_, err := ring.WriteTo(w)
if err != nil && err != io.EOF {
panic(err)
}
w.Close()
}()
composite.Info("hello composite 2!")
cli.Info("hello cli 2!")
composite.With(zap.String("foo", "bar")).Warn("warn composite!")
scanner := bufio.NewScanner(r)
lines := 0
for scanner.Scan() {
fmt.Println("-> ", scanner.Text())
lines++
if lines == 3 {
break
}
}
// Output:
// {"level":"info","msg":"hello cli!"}
// {"level":"info","msg":"hello composite!"}
// {"level":"info","msg":"hello composite 2!"}
// {"level":"info","msg":"hello cli 2!"}
// {"level":"warn","msg":"warn composite!","foo":"bar"}
// -> {"L":"INFO","M":"hello composite!"}
// -> {"L":"INFO","M":"hello composite 2!"}
// -> {"L":"WARN","M":"warn composite!","foo":"bar"}
}
func Example_simple() {
ring := zapring.New(10 * 1024 * 1024) // 10MB ring-buffer
logger := zap.New(ring, zap.Development())
logger.Info("test")
// Output:
}