From a7ab91c2d16837ecf8904a95702d08ea433f2790 Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Tue, 9 Nov 2021 22:45:45 +0100 Subject: [PATCH] syslog format: json support #8 --- config.yml | 2 ++ dnsutils/config.go | 12 ++++++++++++ doc/configuration.md | 2 ++ loggers/stdout.go | 1 - loggers/syslog.go | 15 ++++++++++++++- 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/config.yml b/config.yml index dbb093a3..8bdd103c 100644 --- a/config.yml +++ b/config.yml @@ -239,6 +239,8 @@ loggers: # output text format, please refer to the default text format to see all available directives # use this parameter if you want a specific format text-format: "" + # output format: text|json + mode: text fluentd: # to enable, set the enable to true diff --git a/dnsutils/config.go b/dnsutils/config.go index 2424cd50..c9633394 100644 --- a/dnsutils/config.go +++ b/dnsutils/config.go @@ -7,6 +7,16 @@ import ( "gopkg.in/yaml.v3" ) +func IsValidMode(mode string) bool { + switch mode { + case + "text", + "json": + return true + } + return false +} + type Config struct { Trace struct { Verbose bool `yaml:"verbose"` @@ -122,6 +132,7 @@ type Config struct { Transport string `yaml:"transport"` RemoteAddress string `yaml:"remote-address"` TextFormat string `yaml:"text-format"` + Mode string `yaml:"mode"` } `yaml:"syslog"` Fluentd struct { Enable bool `yaml:"enable"` @@ -248,6 +259,7 @@ func (c *Config) SetDefault() { c.Loggers.Syslog.Transport = "local" c.Loggers.Syslog.RemoteAddress = "127.0.0.1:514" c.Loggers.Syslog.TextFormat = "" + c.Loggers.Syslog.Mode = "text" c.Loggers.Fluentd.Enable = false c.Loggers.Fluentd.RemoteAddress = "127.0.0.1" diff --git a/doc/configuration.md b/doc/configuration.md index 09d45236..7eded616 100644 --- a/doc/configuration.md +++ b/doc/configuration.md @@ -495,6 +495,8 @@ syslog: # output text format, please refer to the default text format to see all available directives # use this parameter if you want a specific format text-format: "" + # output format: text|json + mode: text ``` ### Fluentd Client diff --git a/loggers/stdout.go b/loggers/stdout.go index 780329e2..3dbf9da1 100644 --- a/loggers/stdout.go +++ b/loggers/stdout.go @@ -36,7 +36,6 @@ func NewStdOut(config *dnsutils.Config, console *logger.Logger) *StdOut { } func (c *StdOut) ReadConfig() { - c.mode = c.config.Loggers.Stdout.Mode if len(c.config.Loggers.Stdout.TextFormat) > 0 { c.textFormat = strings.Fields(c.config.Loggers.Stdout.TextFormat) } else { diff --git a/loggers/syslog.go b/loggers/syslog.go index 14484ba8..8aae21ee 100644 --- a/loggers/syslog.go +++ b/loggers/syslog.go @@ -1,6 +1,8 @@ package loggers import ( + "bytes" + "encoding/json" "fmt" "log/syslog" "strings" @@ -69,6 +71,9 @@ func NewSyslog(config *dnsutils.Config, console *logger.Logger) *Syslog { } func (c *Syslog) ReadConfig() { + if !dnsutils.IsValidMode(c.config.Loggers.Syslog.Mode) { + c.logger.Fatal("logger syslog - invalid mode text or json expected") + } severity, err := GetPriority(c.config.Loggers.Syslog.Severity) if err != nil { c.logger.Fatal("logger syslog - invalid severity") @@ -121,6 +126,8 @@ func (o *Syslog) Run() { var syslogconn *syslog.Writer var err error + buffer := new(bytes.Buffer) + if o.config.Loggers.Syslog.Transport == "local" { syslogconn, err = syslog.New(o.facility|o.severity, "") if err != nil { @@ -135,7 +142,13 @@ func (o *Syslog) Run() { o.syslogConn = syslogconn for dm := range o.channel { - o.syslogConn.Write(dm.Bytes(o.textFormat)) + switch o.config.Loggers.Syslog.Mode { + case "text": + o.syslogConn.Write(dm.Bytes(o.textFormat)) + case "json": + json.NewEncoder(buffer).Encode(dm) + o.syslogConn.Write(buffer.Bytes()) + } } o.LogInfo("run terminated")