Add log-format option

This commit is contained in:
Julien Riou 2019-02-16 11:47:30 +01:00
parent fac5099c9b
commit 24eb4fe203
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D
9 changed files with 61 additions and 44 deletions

View file

@ -7,12 +7,14 @@ import (
// Console notifier structure
type Console struct {
format string
sessions chan *base.Session
}
// NewConsole creates a console notifier
func NewConsole(sessions chan *base.Session) Notifier {
func NewConsole(format string, sessions chan *base.Session) Notifier {
return &Console{
format: format,
sessions: sessions,
}
}
@ -21,7 +23,7 @@ func NewConsole(sessions chan *base.Session) Notifier {
func (c *Console) Run() {
log.Info("Starting console notifier")
for session := range c.sessions {
log.Infof("%s\n", session)
log.Info(session.Format(c.format))
}
}

View file

@ -1,25 +1,28 @@
package notifier
import (
"github.com/jouir/pgterminate/base"
"github.com/jouir/pgterminate/log"
"os"
"sync"
"time"
"github.com/jouir/pgterminate/base"
"github.com/jouir/pgterminate/log"
)
// File structure for file notifier
type File struct {
handle *os.File
format string
name string
sessions chan *base.Session
mutex sync.Mutex
}
// NewFile creates a file notifier
func NewFile(name string, sessions chan *base.Session) Notifier {
func NewFile(format string, name string, sessions chan *base.Session) Notifier {
return &File{
name: name,
format: format,
sessions: sessions,
}
}
@ -32,7 +35,7 @@ func (f *File) Run() {
for session := range f.sessions {
timestamp := time.Now().Format(time.RFC3339)
_, err := f.handle.WriteString(timestamp + " " + session.String() + "\n")
_, err := f.handle.WriteString(timestamp + " " + session.Format(f.format) + "\n")
base.Panic(err)
}
}

View file

@ -15,10 +15,10 @@ type Notifier interface {
func NewNotifier(ctx *base.Context) Notifier {
switch ctx.Config.LogDestination {
case "file":
return NewFile(ctx.Config.LogFile, ctx.Sessions)
return NewFile(ctx.Config.LogFile, ctx.Config.LogFormat, ctx.Sessions)
case "syslog":
return NewSyslog(ctx.Config.SyslogFacility, ctx.Config.SyslogIdent, ctx.Sessions)
return NewSyslog(ctx.Config.SyslogFacility, ctx.Config.SyslogIdent, ctx.Config.LogFormat, ctx.Sessions)
default: // console
return NewConsole(ctx.Sessions)
return NewConsole(ctx.Config.LogFormat, ctx.Sessions)
}
}

View file

@ -1,22 +1,23 @@
package notifier
import (
"fmt"
"log/syslog"
"github.com/jouir/pgterminate/base"
"github.com/jouir/pgterminate/log"
"log/syslog"
)
// Syslog notifier
type Syslog struct {
sessions chan *base.Session
ident string
format string
priority syslog.Priority
writer *syslog.Writer
}
// NewSyslog creates a syslog notifier
func NewSyslog(facility string, ident string, sessions chan *base.Session) Notifier {
func NewSyslog(facility string, ident string, format string, sessions chan *base.Session) Notifier {
var priority syslog.Priority
switch facility {
case "LOCAL0":
@ -40,6 +41,7 @@ func NewSyslog(facility string, ident string, sessions chan *base.Session) Notif
sessions: sessions,
ident: ident,
priority: priority,
format: format,
}
}
@ -51,7 +53,7 @@ func (s *Syslog) Run() {
base.Panic(err)
}
for session := range s.sessions {
s.writer.Info(fmt.Sprintf("%s", session))
s.writer.Info(session.Format(s.format))
}
}