mirror of
https://gitlab.alpinelinux.org/alpine/aports.git
synced 2025-07-24 03:35:38 +03:00
93 lines
1.9 KiB
Diff
93 lines
1.9 KiB
Diff
Add "syslog" log format.
|
|
|
|
I didn't contribute this patch to the upstream because I don't wanna
|
|
contribute to any Go projects. However, if you find this patch useful,
|
|
feel free to send it to the upstream under your name.
|
|
|
|
--- a/log/configuration.go
|
|
+++ b/log/configuration.go
|
|
@@ -12,6 +12,7 @@
|
|
FormatRunner = "runner"
|
|
FormatText = "text"
|
|
FormatJSON = "json"
|
|
+ FormatSyslog = "syslog"
|
|
)
|
|
|
|
var (
|
|
@@ -25,7 +26,7 @@
|
|
},
|
|
cli.StringFlag{
|
|
Name: "log-format",
|
|
- Usage: "Choose log format (options: runner, text, json)",
|
|
+ Usage: "Choose log format (options: runner, text, json, syslog)",
|
|
EnvVar: "LOG_FORMAT",
|
|
},
|
|
cli.StringFlag{
|
|
@@ -39,6 +40,7 @@
|
|
FormatRunner: new(RunnerTextFormatter),
|
|
FormatText: new(logrus.TextFormatter),
|
|
FormatJSON: new(logrus.JSONFormatter),
|
|
+ FormatSyslog: new(SyslogFormatter),
|
|
}
|
|
)
|
|
|
|
--- a/log/syslog_formatter.go
|
|
+++ b/log/syslog_formatter.go
|
|
@@ -0,0 +1,57 @@
|
|
+package log
|
|
+
|
|
+import (
|
|
+ "bytes"
|
|
+ "fmt"
|
|
+ "time"
|
|
+
|
|
+ "github.com/sirupsen/logrus"
|
|
+)
|
|
+
|
|
+type SyslogFormatter struct {
|
|
+}
|
|
+
|
|
+func (f *SyslogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
+ var b *bytes.Buffer
|
|
+ if entry.Buffer != nil {
|
|
+ b = entry.Buffer
|
|
+ } else {
|
|
+ b = &bytes.Buffer{}
|
|
+ }
|
|
+
|
|
+ if entry.Message != "" {
|
|
+ b.WriteString(entry.Message)
|
|
+ }
|
|
+ dataWritten := false
|
|
+ for key, val := range entry.Data {
|
|
+ if key == "now" {
|
|
+ continue
|
|
+ }
|
|
+ var stringVal string
|
|
+
|
|
+ switch val := val.(type) {
|
|
+ case time.Time:
|
|
+ stringVal = val.Format(time.RFC3339)
|
|
+ default:
|
|
+ var ok bool
|
|
+ if stringVal, ok = val.(string); !ok {
|
|
+ stringVal = fmt.Sprint(val)
|
|
+ }
|
|
+ }
|
|
+
|
|
+ // The format is inspired by RFC 5424 STRUCTURED-DATA.
|
|
+ if dataWritten {
|
|
+ b.WriteByte(' ')
|
|
+ } else {
|
|
+ b.WriteString(" [meta ")
|
|
+ dataWritten = true
|
|
+ }
|
|
+ b.WriteString(fmt.Sprintf("%s=%q", key, stringVal))
|
|
+ }
|
|
+ if dataWritten {
|
|
+ b.WriteByte(']')
|
|
+ }
|
|
+ b.WriteByte('\n')
|
|
+
|
|
+ return b.Bytes(), nil
|
|
+}
|