-
Notifications
You must be signed in to change notification settings - Fork 572
/
syslog_test.go
108 lines (94 loc) · 2.67 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
// +build !binary_log
// +build !windows
package zerolog
import (
"bytes"
"reflect"
"strings"
"testing"
)
type syslogEvent struct {
level string
msg string
}
type syslogTestWriter struct {
events []syslogEvent
}
func (w *syslogTestWriter) Write(p []byte) (int, error) {
return 0, nil
}
func (w *syslogTestWriter) Trace(m string) error {
w.events = append(w.events, syslogEvent{"Trace", m})
return nil
}
func (w *syslogTestWriter) Debug(m string) error {
w.events = append(w.events, syslogEvent{"Debug", m})
return nil
}
func (w *syslogTestWriter) Info(m string) error {
w.events = append(w.events, syslogEvent{"Info", m})
return nil
}
func (w *syslogTestWriter) Warning(m string) error {
w.events = append(w.events, syslogEvent{"Warning", m})
return nil
}
func (w *syslogTestWriter) Err(m string) error {
w.events = append(w.events, syslogEvent{"Err", m})
return nil
}
func (w *syslogTestWriter) Emerg(m string) error {
w.events = append(w.events, syslogEvent{"Emerg", m})
return nil
}
func (w *syslogTestWriter) Crit(m string) error {
w.events = append(w.events, syslogEvent{"Crit", m})
return nil
}
func TestSyslogWriter(t *testing.T) {
sw := &syslogTestWriter{}
log := New(SyslogLevelWriter(sw))
log.Trace().Msg("trace")
log.Debug().Msg("debug")
log.Info().Msg("info")
log.Warn().Msg("warn")
log.Error().Msg("error")
log.Log().Msg("nolevel")
want := []syslogEvent{
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
{"Info", `{"level":"info","message":"info"}` + "\n"},
{"Warning", `{"level":"warn","message":"warn"}` + "\n"},
{"Err", `{"level":"error","message":"error"}` + "\n"},
{"Info", `{"message":"nolevel"}` + "\n"},
}
if got := sw.events; !reflect.DeepEqual(got, want) {
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
}
}
type testCEEwriter struct {
buf *bytes.Buffer
}
// Only implement one method as we're just testing the prefixing
func (c testCEEwriter) Debug(m string) error { return nil }
func (c testCEEwriter) Info(m string) error {
_, err := c.buf.Write([]byte(m))
return err
}
func (c testCEEwriter) Warning(m string) error { return nil }
func (c testCEEwriter) Err(m string) error { return nil }
func (c testCEEwriter) Emerg(m string) error { return nil }
func (c testCEEwriter) Crit(m string) error { return nil }
func (c testCEEwriter) Write(b []byte) (int, error) {
return c.buf.Write(b)
}
func TestSyslogWriter_WithCEE(t *testing.T) {
var buf bytes.Buffer
sw := testCEEwriter{&buf}
log := New(SyslogCEEWriter(sw))
log.Info().Str("key", "value").Msg("message string")
got := buf.String()
want := "@cee:{"
if !strings.HasPrefix(got, want) {
t.Errorf("Bad CEE message start: want %v, got %v", want, got)
}
}