1
0
Fork 0
forked from boyska/circolog
circolog/formatter/rfc.go

68 lines
1.3 KiB
Go
Raw Permalink Normal View History

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")