syslog logging: fix priority parsing + add test for default facility

This commit is contained in:
Christian Schwarz 2019-03-15 17:44:41 +01:00
parent fc311a9fd6
commit a0f301d700
3 changed files with 42 additions and 34 deletions

View File

@ -259,7 +259,7 @@ type StdoutLoggingOutlet struct {
type SyslogLoggingOutlet struct { type SyslogLoggingOutlet struct {
LoggingOutletCommon `yaml:",inline"` LoggingOutletCommon `yaml:",inline"`
Facility syslog.Priority `yaml:"facility,default=local0"` Facility *SyslogFacility `yaml:"facility,optional,fromdefaults"`
RetryInterval time.Duration `yaml:"retry_interval,positive,default=10s"` RetryInterval time.Duration `yaml:"retry_interval,positive,default=10s"`
} }
@ -286,15 +286,13 @@ type PrometheusMonitoring struct {
Listen string `yaml:"listen"` Listen string `yaml:"listen"`
} }
type SyslogFacilityEnum struct { type SyslogFacility syslog.Priority
Ret interface{}
func (f *SyslogFacility) SetDefault() {
*f = SyslogFacility(syslog.LOG_LOCAL0)
} }
type SyslogFacilityEnumList []SyslogFacilityEnum var _ yaml.Defaulter = (*SyslogFacility)(nil)
type SyslogFacility struct {
Facility syslog.Priority
}
type GlobalControl struct { type GlobalControl struct {
SockPath string `yaml:"sockpath,default=/var/run/zrepl/control"` SockPath string `yaml:"sockpath,default=/var/run/zrepl/control"`
@ -401,30 +399,38 @@ func (t *MonitoringEnum) UnmarshalYAML(u func(interface{}, bool) error) (err err
return return
} }
func (t *SyslogFacilityEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) { func (t *SyslogFacility) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
t.Ret, err = enumUnmarshal(u, map[string]interface{}{ var s string
"kern": &SyslogFacility{syslog.LOG_KERN}, if err := u(&s, true); err != nil {
"user": &SyslogFacility{syslog.LOG_USER}, return err
"mail": &SyslogFacility{syslog.LOG_MAIL}, }
"daemon": &SyslogFacility{syslog.LOG_DAEMON}, var level syslog.Priority
"auth": &SyslogFacility{syslog.LOG_AUTH}, switch s {
"syslog": &SyslogFacility{syslog.LOG_SYSLOG}, case "kern": level = syslog.LOG_KERN
"lpr": &SyslogFacility{syslog.LOG_LPR}, case "user": level = syslog.LOG_USER
"news": &SyslogFacility{syslog.LOG_NEWS}, case "mail": level = syslog.LOG_MAIL
"uucp": &SyslogFacility{syslog.LOG_UUCP}, case "daemon": level = syslog.LOG_DAEMON
"cron": &SyslogFacility{syslog.LOG_CRON}, case "auth": level = syslog.LOG_AUTH
"authpriv": &SyslogFacility{syslog.LOG_AUTHPRIV}, case "syslog": level = syslog.LOG_SYSLOG
"ftp": &SyslogFacility{syslog.LOG_FTP}, case "lpr": level = syslog.LOG_LPR
"local0": &SyslogFacility{syslog.LOG_LOCAL0}, case "news": level = syslog.LOG_NEWS
"local1": &SyslogFacility{syslog.LOG_LOCAL1}, case "uucp": level = syslog.LOG_UUCP
"local2": &SyslogFacility{syslog.LOG_LOCAL2}, case "cron": level = syslog.LOG_CRON
"local3": &SyslogFacility{syslog.LOG_LOCAL3}, case "authpriv": level = syslog.LOG_AUTHPRIV
"local4": &SyslogFacility{syslog.LOG_LOCAL4}, case "ftp": level = syslog.LOG_FTP
"local5": &SyslogFacility{syslog.LOG_LOCAL5}, case "local0": level = syslog.LOG_LOCAL0
"local6": &SyslogFacility{syslog.LOG_LOCAL6}, case "local1": level = syslog.LOG_LOCAL1
"local7": &SyslogFacility{syslog.LOG_LOCAL7}, case "local2": level = syslog.LOG_LOCAL2
}) case "local3": level = syslog.LOG_LOCAL3
return case "local4": level = syslog.LOG_LOCAL4
case "local5": level = syslog.LOG_LOCAL5
case "local6": level = syslog.LOG_LOCAL6
case "local7": level = syslog.LOG_LOCAL7
default:
return fmt.Errorf("invalid syslog level: %q", s)
}
*t = SyslogFacility(level)
return nil
} }
var ConfigFileDefaultLocations = []string{ var ConfigFileDefaultLocations = []string{

View File

@ -80,6 +80,7 @@ func TestSyslogLoggingOutletFacility(t *testing.T) {
Priority syslog.Priority Priority syslog.Priority
} }
syslogFacilitiesPriorities := []SyslogFacilityPriority{ syslogFacilitiesPriorities := []SyslogFacilityPriority{
{"", syslog.LOG_LOCAL0}, // default
{"kern", syslog.LOG_KERN}, {"daemon", syslog.LOG_DAEMON}, {"auth", syslog.LOG_AUTH}, {"kern", syslog.LOG_KERN}, {"daemon", syslog.LOG_DAEMON}, {"auth", syslog.LOG_AUTH},
{"syslog", syslog.LOG_SYSLOG}, {"lpr", syslog.LOG_LPR}, {"news", syslog.LOG_NEWS}, {"syslog", syslog.LOG_SYSLOG}, {"lpr", syslog.LOG_LPR}, {"news", syslog.LOG_NEWS},
{"uucp", syslog.LOG_UUCP}, {"cron", syslog.LOG_CRON}, {"authpriv", syslog.LOG_AUTHPRIV}, {"uucp", syslog.LOG_UUCP}, {"cron", syslog.LOG_CRON}, {"authpriv", syslog.LOG_AUTHPRIV},
@ -99,7 +100,7 @@ global:
`, sFP.Facility) `, sFP.Facility)
conf := testValidGlobalSection(t, logcfg) conf := testValidGlobalSection(t, logcfg)
assert.Equal(t, 1, len(*conf.Global.Logging)) assert.Equal(t, 1, len(*conf.Global.Logging))
assert.Equal(t, sFP.Priority, (*conf.Global.Logging)[0].Ret.(*SyslogLoggingOutlet).Facility) assert.True(t, SyslogFacility(sFP.Priority) == *(*conf.Global.Logging)[0].Ret.(*SyslogLoggingOutlet).Facility)
} }
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"log/syslog"
"os" "os"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
@ -222,7 +223,7 @@ func parseSyslogOutlet(in *config.SyslogLoggingOutlet, formatter EntryFormatter)
out = &SyslogOutlet{} out = &SyslogOutlet{}
out.Formatter = formatter out.Formatter = formatter
out.Formatter.SetMetadataFlags(MetadataNone) out.Formatter.SetMetadataFlags(MetadataNone)
out.Facility = in.Facility out.Facility = syslog.Priority(*in.Facility)
out.RetryInterval = in.RetryInterval out.RetryInterval = in.RetryInterval
return out, nil return out, nil
} }