Refactor SyslogRFC to carry the syslog format directly.

This commit is contained in:
Blallo 2019-05-02 12:05:07 +02:00
parent 0e6b078ad6
commit f73c25d099
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F
2 changed files with 15 additions and 31 deletions

View file

@ -26,7 +26,7 @@ func cleanSocket(socket string) {
func main() { func main() {
var err error var err error
var logFmt formatter.SyslogRFC var logFmt formatter.SyslogRFC
logFmt = formatter.Auto logFmt.Fmt = syslog.Automatic
syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses") 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") // 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") bufsize := flag.Int("buffer-size", 1000, "Number of messages to keep")
@ -47,7 +47,7 @@ func main() {
go hub.Run() go hub.Run()
server := syslog.NewServer() server := syslog.NewServer()
formatter.SetSyslogFormat(server, logFmt) server.SetFormat(logFmt.Fmt)
server.SetHandler(handler) server.SetHandler(handler)
if *syslogSocketPath != "" { if *syslogSocketPath != "" {
if err = server.ListenUnixgram(*syslogSocketPath); err != nil { if err = server.ListenUnixgram(*syslogSocketPath); err != nil {

View file

@ -2,24 +2,14 @@ package formatter
import ( import (
"errors" "errors"
"log"
syslog "gopkg.in/mcuadros/go-syslog.v2" syslog "gopkg.in/mcuadros/go-syslog.v2"
"gopkg.in/mcuadros/go-syslog.v2/format"
) )
type SyslogRFC string // SyslogRFC is the formatter that the server should use
type SyslogRFC struct {
// SetSyslogFormat does the job of setting the server parser to the provided RFC log format. Fmt format.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)
} }
func (rfc *SyslogRFC) Set(v string) error { func (rfc *SyslogRFC) Set(v string) error {
@ -27,41 +17,35 @@ func (rfc *SyslogRFC) Set(v string) error {
if err != nil { if err != nil {
return err return err
} }
*rfc = newval rfc.Fmt = newval
return nil return nil
} }
func (rfc *SyslogRFC) String() string { func (rfc *SyslogRFC) String() string {
switch { switch {
case *rfc == Auto: case rfc.Fmt == syslog.Automatic:
return "auto" return "auto"
case *rfc == RFC3164: case rfc.Fmt == syslog.RFC3164:
return "rfc3164" return "rfc3164"
case *rfc == RFC5424: case rfc.Fmt == syslog.RFC5424:
return "rfc5424" return "rfc5424"
} }
return "" return ""
} }
func parseRFCValue(v string) (SyslogRFC, error) { func parseRFCValue(v string) (format.Format, error) {
switch { switch {
case v == "rfc3164": case v == "rfc3164":
return RFC3164, nil return syslog.RFC3164, nil
case v == "rfc5424": case v == "rfc5424":
return RFC5424, nil return syslog.RFC5424, nil
case v == "auto": case v == "auto":
return Auto, nil return syslog.Automatic, nil
default: default:
return "", ErrRFCNotSupported return nil, ErrRFCNotSupported
} }
} }
const (
RFC3164 = "rfc3164"
RFC5424 = "rfc5424"
Auto = "auto"
)
// ErrRFCNotSupported is raised if the supplied rfc string is // ErrRFCNotSupported is raised if the supplied rfc string is
// not recognized. // not recognized.
var ErrRFCNotSupported = errors.New("RFC not known") var ErrRFCNotSupported = errors.New("RFC not known")