Refactor SyslogRFC to carry the syslog format directly.
This commit is contained in:
parent
0e6b078ad6
commit
f73c25d099
2 changed files with 15 additions and 31 deletions
|
@ -26,7 +26,7 @@ func cleanSocket(socket string) {
|
|||
func main() {
|
||||
var err error
|
||||
var logFmt formatter.SyslogRFC
|
||||
logFmt = formatter.Auto
|
||||
logFmt.Fmt = syslog.Automatic
|
||||
syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses")
|
||||
// dumpSocketPath := flag.String("dump-socket", "/run/buffer.sock", "The socket that user will connect to in order to receive logs")
|
||||
bufsize := flag.Int("buffer-size", 1000, "Number of messages to keep")
|
||||
|
@ -47,7 +47,7 @@ func main() {
|
|||
go hub.Run()
|
||||
|
||||
server := syslog.NewServer()
|
||||
formatter.SetSyslogFormat(server, logFmt)
|
||||
server.SetFormat(logFmt.Fmt)
|
||||
server.SetHandler(handler)
|
||||
if *syslogSocketPath != "" {
|
||||
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
|
||||
|
|
|
@ -2,24 +2,14 @@ package formatter
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
syslog "gopkg.in/mcuadros/go-syslog.v2"
|
||||
"gopkg.in/mcuadros/go-syslog.v2/format"
|
||||
)
|
||||
|
||||
type SyslogRFC string
|
||||
|
||||
// SetSyslogFormat does the job of setting the server parser to the provided RFC log format.
|
||||
func SetSyslogFormat(server *syslog.Server, format SyslogRFC) {
|
||||
switch {
|
||||
case format == Auto:
|
||||
server.SetFormat(syslog.Automatic)
|
||||
case format == RFC3164:
|
||||
server.SetFormat(syslog.RFC3164)
|
||||
case format == RFC5424:
|
||||
server.SetFormat(syslog.RFC5424)
|
||||
}
|
||||
log.Printf("Syslog format set to: %s\n", format)
|
||||
// SyslogRFC is the formatter that the server should use
|
||||
type SyslogRFC struct {
|
||||
Fmt format.Format
|
||||
}
|
||||
|
||||
func (rfc *SyslogRFC) Set(v string) error {
|
||||
|
@ -27,41 +17,35 @@ func (rfc *SyslogRFC) Set(v string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*rfc = newval
|
||||
rfc.Fmt = newval
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rfc *SyslogRFC) String() string {
|
||||
switch {
|
||||
case *rfc == Auto:
|
||||
case rfc.Fmt == syslog.Automatic:
|
||||
return "auto"
|
||||
case *rfc == RFC3164:
|
||||
case rfc.Fmt == syslog.RFC3164:
|
||||
return "rfc3164"
|
||||
case *rfc == RFC5424:
|
||||
case rfc.Fmt == syslog.RFC5424:
|
||||
return "rfc5424"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func parseRFCValue(v string) (SyslogRFC, error) {
|
||||
func parseRFCValue(v string) (format.Format, error) {
|
||||
switch {
|
||||
case v == "rfc3164":
|
||||
return RFC3164, nil
|
||||
return syslog.RFC3164, nil
|
||||
case v == "rfc5424":
|
||||
return RFC5424, nil
|
||||
return syslog.RFC5424, nil
|
||||
case v == "auto":
|
||||
return Auto, nil
|
||||
return syslog.Automatic, nil
|
||||
default:
|
||||
return "", ErrRFCNotSupported
|
||||
return nil, ErrRFCNotSupported
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
RFC3164 = "rfc3164"
|
||||
RFC5424 = "rfc5424"
|
||||
Auto = "auto"
|
||||
)
|
||||
|
||||
// ErrRFCNotSupported is raised if the supplied rfc string is
|
||||
// not recognized.
|
||||
var ErrRFCNotSupported = errors.New("RFC not known")
|
||||
|
|
Loading…
Reference in a new issue