-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
196 lines (182 loc) · 5.28 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package rfc3164
import (
"time"
"github.com/davecgh/go-spew/spew"
)
func init() {
spew.Config.DisableCapacities = true
spew.Config.DisablePointerAddresses = true
}
func output(out interface{}) {
spew.Dump(out)
}
func Example() {
i := []byte(`<13>Dec 2 16:31:03 host app: Test`)
p := NewParser()
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(1),
// Severity: (*uint8)(5),
// Priority: (*uint8)(13),
// Timestamp: (*time.Time)(0000-12-02 16:31:03 +0000 UTC),
// Hostname: (*string)((len=4) "host"),
// Appname: (*string)((len=3) "app"),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=4) "Test")
// }
// })
}
func Example_currentyear() {
i := []byte(`<13>Dec 2 16:31:03 host app: Test`)
p := NewParser(WithYear(CurrentYear{}))
m, _ := p.Parse(i)
// Force year to match the one in the comment below
x, _ := m.(*SyslogMessage)
x.Timestamp = func(t1 *time.Time) *time.Time {
currentY := time.Now().Year()
t2 := t1.AddDate(2021-currentY, 0, 0)
return &t2
}(x.Timestamp)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(1),
// Severity: (*uint8)(5),
// Priority: (*uint8)(13),
// Timestamp: (*time.Time)(2021-12-02 16:31:03 +0000 UTC),
// Hostname: (*string)((len=4) "host"),
// Appname: (*string)((len=3) "app"),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=4) "Test")
// }
// })
}
func Example_withtimezone() {
cet, _ := time.LoadLocation("CET")
i := []byte(`<13>Jan 30 02:08:03 host app: Test`)
p := NewParser(WithTimezone(cet))
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(1),
// Severity: (*uint8)(5),
// Priority: (*uint8)(13),
// Timestamp: (*time.Time)(0000-01-30 03:08:03 +0100 CET),
// Hostname: (*string)((len=4) "host"),
// Appname: (*string)((len=3) "app"),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=4) "Test")
// }
// })
}
func Example_withlocaletimezone() {
pst, _ := time.LoadLocation("America/New_York")
i := []byte(`<13>Nov 22 17:09:42 xxx kernel: [118479565.921459] EXT4-fs warning (device sda8): ext4_dx_add_entry:2006: Directory index full!`)
p := NewParser(WithLocaleTimezone(pst))
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(1),
// Severity: (*uint8)(5),
// Priority: (*uint8)(13),
// Timestamp: (*time.Time)(0000-11-22 17:09:42 -0456 LMT),
// Hostname: (*string)((len=3) "xxx"),
// Appname: (*string)((len=6) "kernel"),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=95) "[118479565.921459] EXT4-fs warning (device sda8): ext4_dx_add_entry:2006: Directory index full!")
// }
// })
}
func Example_withtimezone_and_year() {
est, _ := time.LoadLocation("EST")
i := []byte(`<13>Jan 30 02:08:03 host app: Test`)
p := NewParser(WithTimezone(est), WithYear(Year{YYYY: 1987}))
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(1),
// Severity: (*uint8)(5),
// Priority: (*uint8)(13),
// Timestamp: (*time.Time)(1987-01-29 21:08:03 -0500 EST),
// Hostname: (*string)((len=4) "host"),
// Appname: (*string)((len=3) "app"),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=4) "Test")
// }
// })
}
func Example_besteffort() {
i := []byte(`<13>Dec 2 16:31:03 -`)
p := NewParser(WithBestEffort())
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(1),
// Severity: (*uint8)(5),
// Priority: (*uint8)(13),
// Timestamp: (*time.Time)(0000-12-02 16:31:03 +0000 UTC),
// Hostname: (*string)(<nil>),
// Appname: (*string)(<nil>),
// ProcID: (*string)(<nil>),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=1) "-")
// }
// })
}
func Example_rfc3339timestamp() {
i := []byte(`<28>2019-12-02T16:49:23+02:00 host app[23410]: Test`)
p := NewParser(WithRFC3339())
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(3),
// Severity: (*uint8)(4),
// Priority: (*uint8)(28),
// Timestamp: (*time.Time)(2019-12-02 16:49:23 +0200 +0200),
// Hostname: (*string)((len=4) "host"),
// Appname: (*string)((len=3) "app"),
// ProcID: (*string)((len=5) "23410"),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=4) "Test")
// }
// })
}
func Example_stamp_also_when_rfc3339() {
i := []byte(`<28>Dec 2 16:49:23 host app[23410]: Test`)
p := NewParser(WithYear(Year{YYYY: 2019}), WithRFC3339())
m, _ := p.Parse(i)
output(m)
// Output:
// (*rfc3164.SyslogMessage)({
// Base: (syslog.Base) {
// Facility: (*uint8)(3),
// Severity: (*uint8)(4),
// Priority: (*uint8)(28),
// Timestamp: (*time.Time)(2019-12-02 16:49:23 +0000 UTC),
// Hostname: (*string)((len=4) "host"),
// Appname: (*string)((len=3) "app"),
// ProcID: (*string)((len=5) "23410"),
// MsgID: (*string)(<nil>),
// Message: (*string)((len=4) "Test")
// }
// })
}