From 96ade1c1d7086ee73a76eda17cec5c029a55e8ef Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Sun, 24 Jun 2018 16:39:36 +0200 Subject: [PATCH] Manage log destination explicitly --- base/config.go | 1 + cmd/pgterminate/main.go | 7 ++++++- config.yaml.example | 1 + notifier/notifier.go | 11 ++++++----- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/base/config.go b/base/config.go index cfb528e..9c0d396 100644 --- a/base/config.go +++ b/base/config.go @@ -26,6 +26,7 @@ type Config struct { ConnectTimeout int `yaml:"connect-timeout"` IdleTimeout float64 `yaml:"idle-timeout"` ActiveTimeout float64 `yaml:"active-timeout"` + LogDestination string `yaml:"log-destination"` LogFile string `yaml:"log-file"` PidFile string `yaml:"pid-file"` SyslogIdent string `yaml:"syslog-ident"` diff --git a/cmd/pgterminate/main.go b/cmd/pgterminate/main.go index e47c1cf..c816600 100644 --- a/cmd/pgterminate/main.go +++ b/cmd/pgterminate/main.go @@ -35,6 +35,7 @@ func main() { flag.IntVar(&config.ConnectTimeout, "connect-timeout", 3, "Connection timeout in seconds") flag.Float64Var(&config.IdleTimeout, "idle-timeout", 0, "Time for idle connections to be terminated in seconds") flag.Float64Var(&config.ActiveTimeout, "active-timeout", 0, "Time for active connections to be terminated in seconds") + flag.StringVar(&config.LogDestination, "log-destination", "console", "Log destination between 'console', 'syslog' or 'file'") flag.StringVar(&config.LogFile, "log-file", "", "Write logs to a file") flag.StringVar(&config.PidFile, "pid-file", "", "Write process id into a file") flag.StringVar(&config.SyslogIdent, "syslog-ident", "pgterminate", "Define syslog tag") @@ -66,7 +67,11 @@ func main() { log.Fatalln("Parameter -active-timeout or -idle-timeout required") } - if config.SyslogFacility != "" { + if config.LogDestination != "console" && config.LogDestination != "file" && config.LogDestination != "syslog" { + log.Fatalln("Log destination must be 'console', 'file' or 'syslog'") + } + + if config.LogDestination == "syslog" && config.SyslogFacility != "" { matched, err := regexp.MatchString("^LOCAL[0-7]$", config.SyslogFacility) base.Panic(err) if !matched { diff --git a/config.yaml.example b/config.yaml.example index f10adb7..022357e 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -9,5 +9,6 @@ idle-timeout: 300 active-timeout: 10 log-file: /var/log/pgterminate/pgterminate.log pid-file: /var/run/pgterminate/pgterminate.pid +#log-destination: console|file|syslog #syslog-ident: pgterminate #syslog-facility: LOCAL0 diff --git a/notifier/notifier.go b/notifier/notifier.go index 2c6921e..4281142 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -10,14 +10,15 @@ type Notifier interface { Reload() } -// NewNotifier looks into Config to create a File or Console notifier and pass it +// NewNotifier looks into Config to create a Console, File or Syslog notifier and pass it // the session channel for consuming sessions structs sent by terminator func NewNotifier(ctx *base.Context) Notifier { - if ctx.Config.LogFile != "" { + switch ctx.Config.LogDestination { + case "file": return NewFile(ctx.Config.LogFile, ctx.Sessions) - } - if ctx.Config.SyslogFacility != "" { + case "syslog": return NewSyslog(ctx.Config.SyslogFacility, ctx.Config.SyslogIdent, ctx.Sessions) + default: // console + return NewConsole(ctx.Sessions) } - return NewConsole(ctx.Sessions) }