Skip to content

Commit 858a415

Browse files
authored
Merge pull request #31 from cezarsa/master
Allow extensibility by not exposing types declared in internal pkgs
2 parents d3055b9 + e6dd18c commit 858a415

File tree

9 files changed

+38
-25
lines changed

9 files changed

+38
-25
lines changed

format/automatic.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"strconv"
88

9-
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
109
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc3164"
1110
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424"
1211
)
@@ -57,20 +56,20 @@ func detect(data []byte) (detected int, err error) {
5756
return detectedUnknown, nil
5857
}
5958

60-
func (f *Automatic) GetParser(line []byte) syslogparser.LogParser {
59+
func (f *Automatic) GetParser(line []byte) LogParser {
6160
switch format, _ := detect(line); format {
6261
case detectedRFC3164:
63-
return rfc3164.NewParser(line)
62+
return &parserWrapper{rfc3164.NewParser(line)}
6463
case detectedRFC5424:
65-
return rfc5424.NewParser(line)
64+
return &parserWrapper{rfc5424.NewParser(line)}
6665
default:
6766
// If the line was an RFC6587 line, the splitter should already have removed the length,
6867
// so one of the above two will be chosen if the line is correctly formed. However, it
6968
// may have a second length illegally placed at the start, in which case the detector
7069
// will return detectedRFC6587. The line may also simply be malformed after the length in
7170
// which case we will have detectedUnknown. In this case we return the simplest parser so
7271
// the illegally formatted line is properly handled
73-
return rfc3164.NewParser(line)
72+
return &parserWrapper{rfc3164.NewParser(line)}
7473
}
7574
}
7675

format/format.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@ package format
22

33
import (
44
"bufio"
5+
"time"
56

67
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
78
)
89

10+
type LogParts map[string]interface{}
11+
12+
type LogParser interface {
13+
Parse() error
14+
Dump() LogParts
15+
Location(*time.Location)
16+
}
17+
918
type Format interface {
10-
GetParser([]byte) syslogparser.LogParser
19+
GetParser([]byte) LogParser
1120
GetSplitFunc() bufio.SplitFunc
1221
}
22+
23+
type parserWrapper struct {
24+
syslogparser.LogParser
25+
}
26+
27+
func (w *parserWrapper) Dump() LogParts {
28+
return LogParts(w.LogParser.Dump())
29+
}

format/rfc3164.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package format
33
import (
44
"bufio"
55

6-
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
76
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc3164"
87
)
98

109
type RFC3164 struct{}
1110

12-
func (f *RFC3164) GetParser(line []byte) syslogparser.LogParser {
13-
return rfc3164.NewParser(line)
11+
func (f *RFC3164) GetParser(line []byte) LogParser {
12+
return &parserWrapper{rfc3164.NewParser(line)}
1413
}
1514

1615
func (f *RFC3164) GetSplitFunc() bufio.SplitFunc {

format/rfc5424.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package format
33
import (
44
"bufio"
55

6-
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
76
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424"
87
)
98

109
type RFC5424 struct{}
1110

12-
func (f *RFC5424) GetParser(line []byte) syslogparser.LogParser {
13-
return rfc5424.NewParser(line)
11+
func (f *RFC5424) GetParser(line []byte) LogParser {
12+
return &parserWrapper{rfc5424.NewParser(line)}
1413
}
1514

1615
func (f *RFC5424) GetSplitFunc() bufio.SplitFunc {

format/rfc6587.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import (
55
"bytes"
66
"strconv"
77

8-
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
98
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424"
109
)
1110

1211
type RFC6587 struct{}
1312

14-
func (f *RFC6587) GetParser(line []byte) syslogparser.LogParser {
15-
return rfc5424.NewParser(line)
13+
func (f *RFC6587) GetParser(line []byte) LogParser {
14+
return &parserWrapper{rfc5424.NewParser(line)}
1615
}
1716

1817
func (f *RFC6587) GetSplitFunc() bufio.SplitFunc {

handler.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package syslog
22

33
import (
4-
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
4+
"gopkg.in/mcuadros/go-syslog.v2/format"
55
)
66

7-
type LogParts syslogparser.LogParts
8-
97
//The handler receive every syslog entry at Handle method
108
type Handler interface {
11-
Handle(LogParts, int64, error)
9+
Handle(format.LogParts, int64, error)
1210
}
1311

14-
type LogPartsChannel chan LogParts
12+
type LogPartsChannel chan format.LogParts
1513

1614
//The ChannelHandler will send all the syslog entries into the given channel
1715
type ChannelHandler struct {
@@ -32,6 +30,6 @@ func (h *ChannelHandler) SetChannel(channel LogPartsChannel) {
3230
}
3331

3432
//Syslog entry receiver
35-
func (h *ChannelHandler) Handle(logParts LogParts, messageLength int64, err error) {
33+
func (h *ChannelHandler) Handle(logParts format.LogParts, messageLength int64, err error) {
3634
h.channel <- logParts
3735
}

handler_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package syslog
22

33
import (
44
. "gopkg.in/check.v1"
5+
"gopkg.in/mcuadros/go-syslog.v2/format"
56
)
67

78
type HandlerSuite struct{}
89

910
var _ = Suite(&HandlerSuite{})
1011

1112
func (s *HandlerSuite) TestHandle(c *C) {
12-
logPart := LogParts{"tag": "foo"}
13+
logPart := format.LogParts{"tag": "foo"}
1314

1415
channel := make(LogPartsChannel, 1)
1516
handler := NewChannelHandler(channel)

server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) {
262262
}
263263
logParts["tls_peer"] = tlsPeer
264264

265-
s.handler.Handle(LogParts(logParts), int64(len(line)), err)
265+
s.handler.Handle(logParts, int64(len(line)), err)
266266
}
267267

268268
//Returns the last error

server_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
. "gopkg.in/check.v1"
11+
"gopkg.in/mcuadros/go-syslog.v2/format"
1112
)
1213

1314
func Test(t *testing.T) { TestingT(t) }
@@ -50,12 +51,12 @@ func (s *ServerSuite) TestTailFile(c *C) {
5051
}
5152

5253
type HandlerMock struct {
53-
LastLogParts LogParts
54+
LastLogParts format.LogParts
5455
LastMessageLength int64
5556
LastError error
5657
}
5758

58-
func (s *HandlerMock) Handle(logParts LogParts, msgLen int64, err error) {
59+
func (s *HandlerMock) Handle(logParts format.LogParts, msgLen int64, err error) {
5960
s.LastLogParts = logParts
6061
s.LastMessageLength = msgLen
6162
s.LastError = err

0 commit comments

Comments
 (0)