Skip to content

Commit a127d82

Browse files
ianicmcuadros
authored andcommitted
Add support for timestamp in RFC3339 format (#47)
Default Go log/syslog is sending syslog messages in that format: https://github.com/golang/go/blob/9745eed4fd4160cfbf55e9dbbfa99aca5563b392/src/log/syslog/syslog.go#L294 That is not according to the standard: "The TIMESTAMP field is the local time and is in the format of "Mmm dd hh:mm:ss" (without the quote marks)" (https://tools.ietf.org/html/rfc3164#section-5.3) But it is necessery if we want to parse lines sent by log/syslog. One small fix, time.Stamp is same as those two formats: - "Jan 02 15:04:05", - "Jan 2 15:04:05", + time.Stamp,
1 parent 74a1a0f commit a127d82

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

internal/syslogparser/rfc3164/rfc3164.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,16 @@ func (p *Parser) parseTimestamp() (time.Time, error) {
142142
var sub []byte
143143

144144
tsFmts := []string{
145-
"Jan 02 15:04:05",
146-
"Jan 2 15:04:05",
145+
time.Stamp,
146+
time.RFC3339,
147+
}
148+
// if timestamps starts with numeric try formats with different order
149+
// it is more likely that timestamp is in RFC3339 format then
150+
if c := p.buff[p.cursor]; c > '0' && c < '9' {
151+
tsFmts = []string{
152+
time.RFC3339,
153+
time.Stamp,
154+
}
147155
}
148156

149157
found := false
@@ -163,7 +171,7 @@ func (p *Parser) parseTimestamp() (time.Time, error) {
163171
}
164172

165173
if !found {
166-
p.cursor = tsFmtLen
174+
p.cursor = len(time.Stamp)
167175

168176
// XXX : If the timestamp is invalid we try to push the cursor one byte
169177
// XXX : further, in case it is a space

internal/syslogparser/rfc3164/rfc3164_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,46 @@ func (s *Rfc3164TestSuite) TestParseHeader_Valid(c *C) {
130130
}
131131

132132
s.assertRfc3164Header(c, hdr, buff, 25, nil)
133+
134+
// expected header for next two tests
135+
hdr = header{
136+
timestamp: time.Date(now.Year(), time.October, 1, 22, 14, 15, 0, time.UTC),
137+
hostname: "mymachine",
138+
}
139+
// day with leading zero
140+
buff = []byte("Oct 01 22:14:15 mymachine ")
141+
s.assertRfc3164Header(c, hdr, buff, 25, nil)
142+
// day with leading space
143+
buff = []byte("Oct 1 22:14:15 mymachine ")
144+
s.assertRfc3164Header(c, hdr, buff, 25, nil)
145+
146+
}
147+
148+
func (s *Rfc3164TestSuite) TestParseHeader_RFC3339Timestamp(c *C) {
149+
buff := []byte("2018-01-12T22:14:15+00:00 mymachine app[101]: msg")
150+
hdr := header{
151+
timestamp: time.Date(2018, time.January, 12, 22, 14, 15, 0, time.UTC),
152+
hostname: "mymachine",
153+
}
154+
s.assertRfc3164Header(c, hdr, buff, 35, nil)
155+
}
156+
157+
func (s *Rfc3164TestSuite) TestParser_ValidRFC3339Timestamp(c *C) {
158+
buff := []byte("<34>2018-01-12T22:14:15+00:00 mymachine app[101]: msg")
159+
p := NewParser(buff)
160+
err := p.Parse()
161+
c.Assert(err, IsNil)
162+
obtained := p.Dump()
163+
expected := syslogparser.LogParts{
164+
"timestamp": time.Date(2018, time.January, 12, 22, 14, 15, 0, time.UTC),
165+
"hostname": "mymachine",
166+
"tag": "app",
167+
"content": "msg",
168+
"priority": 34,
169+
"facility": 4,
170+
"severity": 2,
171+
}
172+
c.Assert(obtained, DeepEquals, expected)
133173
}
134174

135175
func (s *Rfc3164TestSuite) TestParseHeader_InvalidTimestamp(c *C) {

0 commit comments

Comments
 (0)