Skip to content

Commit

Permalink
syslog format: json support #8
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Nov 9, 2021
1 parent c4142fb commit a7ab91c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions dnsutils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion loggers/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion loggers/syslog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package loggers

import (
"bytes"
"encoding/json"
"fmt"
"log/syslog"
"strings"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down

0 comments on commit a7ab91c

Please sign in to comment.