Manage log destination explicitly

This commit is contained in:
Julien Riou 2018-06-24 16:39:36 +02:00
parent 85207e0a77
commit 96ade1c1d7
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D
4 changed files with 14 additions and 6 deletions

View file

@ -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"`

View file

@ -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 {

View file

@ -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

View file

@ -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)
}