-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
51 lines (44 loc) · 1.13 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
package zap_net_sink_test
import (
"encoding/json"
"fmt"
"net"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func Example() {
// Setup UDP server
laddr, err := net.ResolveUDPAddr("udp", "0.0.0.0:1234")
if err != nil {
panic(err)
}
udpServer, err := net.ListenUDP("udp", laddr)
if err != nil {
panic(err)
}
defer udpServer.Close()
// Pass all JSON messages that the UDP server receives to a map[string]interface{} channel.
udpDec := json.NewDecoder(udpServer)
udpC := make(chan map[string]interface{}, 1)
go func() {
for {
var v map[string]interface{}
if err := udpDec.Decode(&v); err != nil {
return
}
udpC <- v
}
}()
writer, cleanup, err := zap.Open("udp://127.0.0.1:1234")
if err != nil {
panic(err)
}
defer cleanup()
logger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), writer, zap.DebugLevel)).Sugar()
logger.Infow("hello", "from", "Example()")
msg := <-udpC
msg["ts"] = "overridden to make tests pass"
fmt.Printf("%#v\n", msg)
// Output:
// map[string]interface {}{"from":"Example()", "level":"info", "msg":"hello", "ts":"overridden to make tests pass"}
}