| 1 | Add "syslog" log format.
|
| 2 |
|
| 3 | I didn't contribute this patch to the upstream because I don't wanna
|
| 4 | contribute to any Go projects. However, if you find this patch useful,
|
| 5 | feel free to send it to the upstream under your name.
|
| 6 |
|
| 7 | diff --git a/log/configuration.go b/log/configuration.go
|
| 8 | index a756242b8..cbe4dabcd 100644
|
| 9 | |
| 10 | |
| 11 | @@ -13,6 +13,7 @@ const (
|
| 12 | FormatRunner = "runner"
|
| 13 | FormatText = "text"
|
| 14 | FormatJSON = "json"
|
| 15 | + FormatSyslog = "syslog"
|
| 16 | )
|
| 17 |
|
| 18 | var (
|
| 19 | @@ -26,7 +27,7 @@ var (
|
| 20 | },
|
| 21 | cli.StringFlag{
|
| 22 | Name: "log-format",
|
| 23 | - Usage: "Choose log format (options: runner, text, json)",
|
| 24 | + Usage: "Choose log format (options: runner, text, json, syslog)",
|
| 25 | EnvVar: "LOG_FORMAT",
|
| 26 | },
|
| 27 | cli.StringFlag{
|
| 28 | @@ -44,6 +45,7 @@ var (
|
| 29 | FormatJSON: &logrus.JSONFormatter{
|
| 30 | TimestampFormat: time.RFC3339Nano,
|
| 31 | },
|
| 32 | + FormatSyslog: new(SyslogFormatter),
|
| 33 | }
|
| 34 | )
|
| 35 |
|
| 36 | diff --git a/log/syslog_formatter.go b/log/syslog_formatter.go
|
| 37 | new file mode 100644
|
| 38 | index 000000000..fe60e0fc7
|
| 39 | |
| 40 | |
| 41 | @@ -0,0 +1,57 @@
|
| 42 | +package log
|
| 43 | +
|
| 44 | +import (
|
| 45 | + "bytes"
|
| 46 | + "fmt"
|
| 47 | + "time"
|
| 48 | +
|
| 49 | + "github.com/sirupsen/logrus"
|
| 50 | +)
|
| 51 | +
|
| 52 | +type SyslogFormatter struct {
|
| 53 | +}
|
| 54 | +
|
| 55 | +func (f *SyslogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
| 56 | + var b *bytes.Buffer
|
| 57 | + if entry.Buffer != nil {
|
| 58 | + b = entry.Buffer
|
| 59 | + } else {
|
| 60 | + b = &bytes.Buffer{}
|
| 61 | + }
|
| 62 | +
|
| 63 | + if entry.Message != "" {
|
| 64 | + b.WriteString(entry.Message)
|
| 65 | + }
|
| 66 | + dataWritten := false
|
| 67 | + for key, val := range entry.Data {
|
| 68 | + if key == "now" {
|
| 69 | + continue
|
| 70 | + }
|
| 71 | + var stringVal string
|
| 72 | +
|
| 73 | + switch val := val.(type) {
|
| 74 | + case time.Time:
|
| 75 | + stringVal = val.Format(time.RFC3339)
|
| 76 | + default:
|
| 77 | + var ok bool
|
| 78 | + if stringVal, ok = val.(string); !ok {
|
| 79 | + stringVal = fmt.Sprint(val)
|
| 80 | + }
|
| 81 | + }
|
| 82 | +
|
| 83 | + // The format is inspired by RFC 5424 STRUCTURED-DATA.
|
| 84 | + if dataWritten {
|
| 85 | + b.WriteByte(' ')
|
| 86 | + } else {
|
| 87 | + b.WriteString(" [meta ")
|
| 88 | + dataWritten = true
|
| 89 | + }
|
| 90 | + b.WriteString(fmt.Sprintf("%s=%q", key, stringVal))
|
| 91 | + }
|
| 92 | + if dataWritten {
|
| 93 | + b.WriteByte(']')
|
| 94 | + }
|
| 95 | + b.WriteByte('\n')
|
| 96 | +
|
| 97 | + return b.Bytes(), nil
|
| 98 | +}
|