forked from phuslu/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog_test.go
113 lines (93 loc) · 3.68 KB
/
syslog_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
package log
import (
"net"
"os"
"testing"
"time"
)
func TestSyslogWriterTCP(t *testing.T) {
w := &SyslogWriter{
Network: "udp",
Address: "10.0.0.2:543",
Tag: "",
Dial: net.Dial,
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
_, _ = wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
}
_, err := wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
if err != nil {
t.Errorf("write syslog writer error: %+v", err)
}
_, err = wlprintf(w, InfoLevel, "a long long long long message.\n")
if err != nil {
t.Errorf("write syslog writer error: %+v", err)
}
w.Close()
w.Close()
_, err = wlprintf(w, InfoLevel, "a long long long long message again.\n")
if err != nil {
t.Errorf("write syslog writer error: %+v", err)
}
}
func TestSyslogWriterTCPError(t *testing.T) {
w := &SyslogWriter{
Network: "tcp",
Address: "127.0.0.1:601",
Tag: "",
Dial: net.Dial,
}
_, _ = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
w.Close()
_, _ = wlprintf(w, InfoLevel, "a long long long long message again.\n")
}
func TestSyslogWriterUnix(t *testing.T) {
const sockname = "/tmp/go-tmp-null.sock"
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: sockname, Net: "unixgram"})
if err != nil {
t.Errorf("listen error: %+v", err)
return
}
defer os.Remove(sockname)
go func() {
var data [512]byte
for {
buf := data[:]
n, uaddr, err := conn.ReadFromUnix(buf)
if err != nil {
t.Logf("listen: error: %v\n", err)
} else {
t.Logf("listen: received %v bytes from %+v\n", n, uaddr)
}
time.Sleep(10 * time.Millisecond)
}
}()
w := &SyslogWriter{
Network: "unixgram",
Address: "/tmp/go-tmp-null.sock",
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
_, _ = wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
}
_, err = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
if err != nil {
t.Errorf("write syslog writer error: %+v", err)
}
_, err = wlprintf(w, InfoLevel, "a long long long long message.\n")
if err != nil {
t.Errorf("write syslog writer error: %+v", err)
}
w.Close()
w.Close()
_, err = wlprintf(w, InfoLevel, "a long long long long message again.\n")
if err != nil {
t.Errorf("write syslog writer error: %+v", err)
}
os.Remove(sockname)
_, err = wlprintf(w, InfoLevel, "a long long long long message again.\n")
t.Logf("write syslog writer error: %+v", err)
}