forked from boyska/circolog
39 lines
1,001 B
Go
39 lines
1,001 B
Go
package circolog
|
|
|
|
import (
|
|
"bytes"
|
|
"text/template"
|
|
"time"
|
|
|
|
"gopkg.in/mcuadros/go-syslog.v2/format"
|
|
)
|
|
|
|
// Formatter is an interface, so that multiple implementations can exist
|
|
type Formatter func(format.LogParts) string
|
|
|
|
var tmplFuncs template.FuncMap
|
|
var syslogTmpl *template.Template
|
|
|
|
func init() {
|
|
tmplFuncs := template.FuncMap{
|
|
"dateFormat": func(dt time.Time, fmt string) string {
|
|
return dt.Format(fmt)
|
|
},
|
|
"rfc822": func(dt time.Time) string {
|
|
return dt.Format(time.RFC822)
|
|
},
|
|
}
|
|
syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse(
|
|
"{{rfc822 (index . \"timestamp\")}} {{index . \"hostname\"}} " +
|
|
"{{index . \"app_name\"}}" +
|
|
"{{ if (ne (index . \"proc_id\") \"-\")}}[{{index . \"proc_id\"}}]{{end}}: " +
|
|
"{{index . \"message\"}}",
|
|
))
|
|
}
|
|
|
|
// FormatSyslog format a message in the typical format used in /var/log/messages
|
|
func FormatSyslog(msg format.LogParts) string {
|
|
var buf bytes.Buffer
|
|
syslogTmpl.Execute(&buf, msg)
|
|
return buf.String()
|
|
}
|