Skip to content

Commit 757a9da

Browse files
authored
Merge pull request #19 from eenblam/eenblam-fixes
Fix #16 & #17
2 parents 323c7ad + 775f53b commit 757a9da

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

rfc3164/rfc3164.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77
)
88

99
type Parser struct {
10-
buff []byte
11-
cursor int
12-
l int
13-
priority syslogparser.Priority
14-
version int
15-
header header
16-
message rfc3164message
17-
location *time.Location
18-
hostname string
10+
buff []byte
11+
cursor int
12+
l int
13+
priority syslogparser.Priority
14+
version int
15+
header header
16+
message rfc3164message
17+
location *time.Location
18+
hostname string
19+
ParsePriority bool
1920
}
2021

2122
type header struct {
@@ -30,10 +31,11 @@ type rfc3164message struct {
3031

3132
func NewParser(buff []byte) *Parser {
3233
return &Parser{
33-
buff: buff,
34-
cursor: 0,
35-
l: len(buff),
36-
location: time.UTC,
34+
buff: buff,
35+
cursor: 0,
36+
l: len(buff),
37+
location: time.UTC,
38+
ParsePriority: true,
3739
}
3840
}
3941

@@ -46,9 +48,18 @@ func (p *Parser) Hostname(hostname string) {
4648
}
4749

4850
func (p *Parser) Parse() error {
49-
pri, err := p.parsePriority()
50-
if err != nil {
51-
return err
51+
if p.ParsePriority {
52+
pri, err := p.parsePriority()
53+
if err != nil {
54+
return err
55+
}
56+
p.priority = pri
57+
} else {
58+
p.priority = syslogparser.Priority{
59+
0,
60+
syslogparser.Facility{0},
61+
syslogparser.Severity{0},
62+
}
5263
}
5364

5465
hdr, err := p.parseHeader()
@@ -65,7 +76,6 @@ func (p *Parser) Parse() error {
6576
return err
6677
}
6778

68-
p.priority = pri
6979
p.version = syslogparser.NO_VERSION
7080
p.header = hdr
7181
p.message = msg
@@ -74,15 +84,18 @@ func (p *Parser) Parse() error {
7484
}
7585

7686
func (p *Parser) Dump() syslogparser.LogParts {
77-
return syslogparser.LogParts{
87+
parts := syslogparser.LogParts{
7888
"timestamp": p.header.timestamp,
7989
"hostname": p.header.hostname,
8090
"tag": p.message.tag,
8191
"content": p.message.content,
82-
"priority": p.priority.P,
83-
"facility": p.priority.F.Value,
84-
"severity": p.priority.S.Value,
8592
}
93+
if p.ParsePriority {
94+
parts["priority"] = p.priority.P
95+
parts["facility"] = p.priority.F.Value
96+
parts["severity"] = p.priority.S.Value
97+
}
98+
return parts
8699
}
87100

88101
func (p *Parser) parsePriority() (syslogparser.Priority, error) {

rfc3164/rfc3164_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/jeromer/syslogparser"
9-
. "launchpad.net/gocheck"
9+
. "gopkg.in/check.v1"
1010
)
1111

1212
// Hooks up gocheck into the gotest runner.
@@ -28,10 +28,11 @@ func (s *Rfc3164TestSuite) TestParser_Valid(c *C) {
2828

2929
p := NewParser(buff)
3030
expectedP := &Parser{
31-
buff: buff,
32-
cursor: 0,
33-
l: len(buff),
34-
location: time.UTC,
31+
buff: buff,
32+
cursor: 0,
33+
l: len(buff),
34+
location: time.UTC,
35+
ParsePriority: true,
3536
}
3637

3738
c.Assert(p, DeepEquals, expectedP)
@@ -55,6 +56,37 @@ func (s *Rfc3164TestSuite) TestParser_Valid(c *C) {
5556
c.Assert(obtained, DeepEquals, expected)
5657
}
5758

59+
func (s *Rfc3164TestSuite) TestParser_WithoutPriority(c *C) {
60+
buff := []byte("Oct 11 22:14:15 mymachine very.large.syslog.message.tag: 'su root' failed for lonvick on /dev/pts/8")
61+
62+
p := NewParser(buff)
63+
p.ParsePriority = false
64+
expectedP := &Parser{
65+
buff: buff,
66+
cursor: 0,
67+
l: len(buff),
68+
location: time.UTC,
69+
ParsePriority: false,
70+
}
71+
72+
c.Assert(p, DeepEquals, expectedP)
73+
74+
err := p.Parse()
75+
c.Assert(err, IsNil)
76+
77+
now := time.Now()
78+
79+
obtained := p.Dump()
80+
expected := syslogparser.LogParts{
81+
"timestamp": time.Date(now.Year(), time.October, 11, 22, 14, 15, 0, time.UTC),
82+
"hostname": "mymachine",
83+
"tag": "very.large.syslog.message.tag",
84+
"content": "'su root' failed for lonvick on /dev/pts/8",
85+
}
86+
87+
c.Assert(obtained, DeepEquals, expected)
88+
}
89+
5890
func (s *Rfc3164TestSuite) TestParseWithout_Hostname(c *C) {
5991
buff := []byte("<30>Jun 23 13:17:42 chronyd[1119]: Selected source 192.168.65.1")
6092

rfc5424/rfc5424_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/jeromer/syslogparser"
9-
. "launchpad.net/gocheck"
9+
. "gopkg.in/check.v1"
1010
)
1111

1212
// Hooks up gocheck into the gotest runner.

syslogparser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package syslogparser
22

33
import (
4-
. "launchpad.net/gocheck"
4+
. "gopkg.in/check.v1"
55
"testing"
66
)
77

0 commit comments

Comments
 (0)