1
0
Fork 0
mirror of https://github.com/Luzifer/elastic_cron.git synced 2024-11-08 22:00:13 +00:00

Allow log format to be configured

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-04-02 14:06:41 +02:00
parent 2809935b64
commit eb7c01ab2f
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 40 additions and 13 deletions

View file

@ -27,6 +27,7 @@ var (
type cronConfig struct { type cronConfig struct {
RSyslogTarget string `yaml:"rsyslog_target"` RSyslogTarget string `yaml:"rsyslog_target"`
LogTemplate string `yaml:"log_template"`
Jobs []cronJob `yaml:"jobs"` Jobs []cronJob `yaml:"jobs"`
} }
@ -52,7 +53,9 @@ func main() {
log.Fatalf("Unable to read config file: %s") log.Fatalf("Unable to read config file: %s")
} }
cc := cronConfig{} cc := cronConfig{
LogTemplate: `<{{ syslogpri .Severity }}>{{ .Date.Format "Jan 02 15:04:05" }} {{ .Hostname }} {{ .JobName }}: {{ .Message }}`,
}
if err := yaml.Unmarshal(body, &cc); err != nil { if err := yaml.Unmarshal(body, &cc); err != nil {
log.Fatalf("Unable to parse config file: %s", err) log.Fatalf("Unable to parse config file: %s", err)
} }
@ -68,7 +71,7 @@ func main() {
c.Start() c.Start()
logadapter, err := NewSyslogAdapter(cc.RSyslogTarget) logadapter, err := NewSyslogAdapter(cc.RSyslogTarget, cc.LogTemplate)
if err != nil { if err != nil {
log.Fatalf("Unable to open syslog connection: %s", err) log.Fatalf("Unable to open syslog connection: %s", err)
} }

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"text/template"
"time" "time"
"github.com/cenkalti/backoff" "github.com/cenkalti/backoff"
@ -15,9 +16,10 @@ const (
tcpDialTimeout = 5 * time.Second tcpDialTimeout = 5 * time.Second
) )
func NewSyslogAdapter(address string) (*SyslogAdapter, error) { func NewSyslogAdapter(address, logFormat string) (*SyslogAdapter, error) {
return &SyslogAdapter{ return &SyslogAdapter{
address: address, address: address,
logFormat: logFormat,
dialer: &net.Dialer{ dialer: &net.Dialer{
Timeout: tcpDialTimeout, Timeout: tcpDialTimeout,
}, },
@ -32,8 +34,32 @@ type message struct {
} }
type SyslogAdapter struct { type SyslogAdapter struct {
address string address string
dialer *net.Dialer logFormat string
dialer *net.Dialer
}
func (a *SyslogAdapter) formatMessage(m *message) (string, error) {
t, err := template.New("logFormat").Funcs(template.FuncMap{
"syslogpri": func(pri int) int { return 16*8 + pri },
}).Parse(a.logFormat)
if err != nil {
return "", err
}
buf := bytes.NewBufferString("")
if err := t.Execute(buf, map[string]interface{}{
"Date": m.Date,
"JobName": m.JobName,
"Hostname": cfg.Hostname,
"Message": m.Message,
"Severity": m.Severity,
}); err != nil {
return "", err
}
return buf.String(), nil
} }
func (a *SyslogAdapter) Stream(logstream chan *message) { func (a *SyslogAdapter) Stream(logstream chan *message) {
@ -49,13 +75,11 @@ func (a *SyslogAdapter) Stream(logstream chan *message) {
for msg := range logstream { for msg := range logstream {
b.Reset() b.Reset()
fmt.Fprintf(b, "<%d>%s %s %s: %s\n", msgLine, err := a.formatMessage(msg)
16*8+msg.Severity, if err != nil {
msg.Date.Format("Jan 02 15:04:05"), return err
cfg.Hostname, }
msg.JobName, fmt.Fprintln(b, msgLine)
msg.Message,
)
if err := conn.SetDeadline(time.Now().Add(readWriteTimeout)); err != nil { if err := conn.SetDeadline(time.Now().Add(readWriteTimeout)); err != nil {
fmt.Printf("syslog: Unable to set deadline: %s\n", err) fmt.Printf("syslog: Unable to set deadline: %s\n", err)