-
Notifications
You must be signed in to change notification settings - Fork 46
/
journal_test.go
60 lines (49 loc) · 1.76 KB
/
journal_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
//go:build linux
package log
import (
"net"
"os"
"testing"
"time"
)
// journalctl -o verbose -f
func TestJournalWriter(t *testing.T) {
w := &JournalWriter{}
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, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
_, _ = wlprintf(w, InfoLevel, "a long long long long message.\n")
w.Close()
}
func TestJournalWriterError(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 := &JournalWriter{
JournalSocket: sockname,
}
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, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
_, _ = wlprintf(w, InfoLevel, "a long long long long message.\n")
w.Close()
}