From 55827916bbab2145e28de15a7496c4ca0ff7202c Mon Sep 17 00:00:00 2001 From: Blallo Date: Wed, 1 May 2019 15:55:41 +0200 Subject: [PATCH] Add flag to set syslog format. --- cmd/circologd/main.go | 7 ++++++- formatter/rfc.go | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 formatter/rfc.go diff --git a/cmd/circologd/main.go b/cmd/circologd/main.go index f0a2de2..308b0ef 100644 --- a/cmd/circologd/main.go +++ b/cmd/circologd/main.go @@ -12,6 +12,7 @@ import ( "time" "git.lattuga.net/boyska/circolog" + "git.lattuga.net/boyska/circolog/formatter" "github.com/coreos/go-systemd/daemon" syslog "gopkg.in/mcuadros/go-syslog.v2" ) @@ -24,6 +25,8 @@ func cleanSocket(socket string) { func main() { var err error + var logFmt formatter.SyslogRFC + logFmt.Format = 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") @@ -31,6 +34,7 @@ func main() { queryAddr := flag.String("query-addr", "127.0.0.1:9080", "Address:port where to bind the query service") querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!") ctlSocket := flag.String("ctl-socket", "/tmp/circologd-ctl.sock", "Path to a unix domain socket for the control server; leave empty to disable") + flag.Var(&logFmt, "log-fmt", "Log messages format. If not set, defaults to automatic choice. Allowed values: rfc3164, rfc5424, auto.") verbose := flag.Bool("verbose", false, "Print more output executing the daemon") debug := flag.Bool("debug", false, "Print debugging info executing the daemon") flag.Parse() @@ -43,7 +47,8 @@ func main() { go hub.Run() server := syslog.NewServer() - server.SetFormat(syslog.Automatic) + server.SetFormat(logFmt.Format) + fmt.Printf("Syslog format set to: %s\n", logFmt.String()) server.SetHandler(handler) if *syslogSocketPath != "" { if err = server.ListenUnixgram(*syslogSocketPath); err != nil { diff --git a/formatter/rfc.go b/formatter/rfc.go new file mode 100644 index 0000000..3196892 --- /dev/null +++ b/formatter/rfc.go @@ -0,0 +1,49 @@ +package formatter + +import ( + "errors" + + syslog "gopkg.in/mcuadros/go-syslog.v2" + "gopkg.in/mcuadros/go-syslog.v2/format" +) + +// SyslogRFC is the formatter that the server should use +type SyslogRFC struct{ format.Format } + +func (rfc *SyslogRFC) Set(v string) error { + newval, err := parseRFCValue(v) + if err != nil { + return err + } + rfc.Format = newval + return nil +} + +func (rfc *SyslogRFC) String() string { + switch { + case rfc.Format == syslog.Automatic: + return "auto" + case rfc.Format == syslog.RFC3164: + return "rfc3164" + case rfc.Format == syslog.RFC5424: + return "rfc5424" + } + return "" +} + +func parseRFCValue(v string) (format.Format, error) { + switch { + case v == "rfc3164": + return syslog.RFC3164, nil + case v == "rfc5424": + return syslog.RFC5424, nil + case v == "auto": + return syslog.Automatic, nil + default: + return nil, ErrRFCNotSupported + } +} + +// ErrRFCNotSupported is raised if the supplied rfc string is +// not recognized. +var ErrRFCNotSupported = errors.New("RFC not known")