Refactor log-fmt as flag.Value. Move in formatter pkg.
This commit is contained in:
parent
76a6381516
commit
0e6b078ad6
2 changed files with 72 additions and 16 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.lattuga.net/boyska/circolog"
|
"git.lattuga.net/boyska/circolog"
|
||||||
|
"git.lattuga.net/boyska/circolog/formatter"
|
||||||
"github.com/coreos/go-systemd/daemon"
|
"github.com/coreos/go-systemd/daemon"
|
||||||
syslog "gopkg.in/mcuadros/go-syslog.v2"
|
syslog "gopkg.in/mcuadros/go-syslog.v2"
|
||||||
)
|
)
|
||||||
|
@ -22,22 +23,10 @@ func cleanSocket(socket string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSyslogFormat(server *syslog.Server, format string) {
|
|
||||||
switch {
|
|
||||||
case format == "auto":
|
|
||||||
server.SetFormat(syslog.Automatic)
|
|
||||||
case format == "rfc3164":
|
|
||||||
server.SetFormat(syslog.RFC3164)
|
|
||||||
case format == "rfc5424":
|
|
||||||
server.SetFormat(syslog.RFC5424)
|
|
||||||
}
|
|
||||||
// TODO: propose a patch to mcuadros/go-syslog.v2
|
|
||||||
// to get the format from the server itself.
|
|
||||||
log.Printf("Syslog format set to: %s\n", format)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
var logFmt formatter.SyslogRFC
|
||||||
|
logFmt = formatter.Auto
|
||||||
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")
|
||||||
|
@ -45,7 +34,7 @@ func main() {
|
||||||
queryAddr := flag.String("query-addr", "127.0.0.1:9080", "Address:port where to bind the query service")
|
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!")
|
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")
|
ctlSocket := flag.String("ctl-socket", "/tmp/circologd-ctl.sock", "Path to a unix domain socket for the control server; leave empty to disable")
|
||||||
logFmt := flag.String("log-fmt", "auto", "Log messages format. If not set, defaults to automatic choice. Allowed values: rfc3164, rfc5424.")
|
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")
|
verbose := flag.Bool("verbose", false, "Print more output executing the daemon")
|
||||||
debug := flag.Bool("debug", false, "Print debugging info executing the daemon")
|
debug := flag.Bool("debug", false, "Print debugging info executing the daemon")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -58,7 +47,7 @@ func main() {
|
||||||
go hub.Run()
|
go hub.Run()
|
||||||
|
|
||||||
server := syslog.NewServer()
|
server := syslog.NewServer()
|
||||||
setSyslogFormat(server, *logFmt)
|
formatter.SetSyslogFormat(server, logFmt)
|
||||||
server.SetHandler(handler)
|
server.SetHandler(handler)
|
||||||
if *syslogSocketPath != "" {
|
if *syslogSocketPath != "" {
|
||||||
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
|
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
|
||||||
|
|
67
formatter/rfc.go
Normal file
67
formatter/rfc.go
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
package formatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
syslog "gopkg.in/mcuadros/go-syslog.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rfc *SyslogRFC) Set(v string) error {
|
||||||
|
newval, err := parseRFCValue(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*rfc = newval
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rfc *SyslogRFC) String() string {
|
||||||
|
switch {
|
||||||
|
case *rfc == Auto:
|
||||||
|
return "auto"
|
||||||
|
case *rfc == RFC3164:
|
||||||
|
return "rfc3164"
|
||||||
|
case *rfc == RFC5424:
|
||||||
|
return "rfc5424"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRFCValue(v string) (SyslogRFC, error) {
|
||||||
|
switch {
|
||||||
|
case v == "rfc3164":
|
||||||
|
return RFC3164, nil
|
||||||
|
case v == "rfc5424":
|
||||||
|
return RFC5424, nil
|
||||||
|
case v == "auto":
|
||||||
|
return Auto, nil
|
||||||
|
default:
|
||||||
|
return "", 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