From 1bee1ae2048051b52bc5b5084808a0418c0def04 Mon Sep 17 00:00:00 2001 From: Sambhav Saggi <17993169+sambhavsaggi@users.noreply.github.com> Date: Tue, 17 Aug 2021 11:18:51 -0400 Subject: [PATCH] Syslog integration (#1952) * Syslog integration, part 1 * Add protocol, make sure syslog actually logs * Make golangci-lint happy about shadow variables * Add syslog tag, wrap syslog in logLevelHook --- internal/log.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/log.go b/internal/log.go index d2b233c5..f0656d7d 100644 --- a/internal/log.go +++ b/internal/log.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "io" + "log/syslog" "net/http" "os" "path" @@ -30,6 +31,7 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dugong" "github.com/sirupsen/logrus" + lSyslog "github.com/sirupsen/logrus/hooks/syslog" ) type utcFormatter struct { @@ -128,6 +130,9 @@ func SetupHookLogging(hooks []config.LogrusHook, componentName string) { case "file": checkFileHookParams(hook.Params) setupFileHook(hook, level, componentName) + case "syslog": + checkSyslogHookParams(hook.Params) + setupSyslogHook(hook, level, componentName) default: logrus.Fatalf("Unrecognised logging hook type: %s", hook.Type) } @@ -173,6 +178,34 @@ func setupFileHook(hook config.LogrusHook, level logrus.Level, componentName str }) } +func checkSyslogHookParams(params map[string]interface{}) { + addr, ok := params["address"] + if !ok { + logrus.Fatalf("Expecting a parameter \"address\" for logging hook of type \"syslog\"") + } + + if _, ok := addr.(string); !ok { + logrus.Fatalf("Parameter \"address\" for logging hook of type \"syslog\" should be a string") + } + + proto, ok2 := params["protocol"] + if !ok2 { + logrus.Fatalf("Expecting a parameter \"protocol\" for logging hook of type \"syslog\"") + } + + if _, ok2 := proto.(string); !ok2 { + logrus.Fatalf("Parameter \"protocol\" for logging hook of type \"syslog\" should be a string") + } + +} + +func setupSyslogHook(hook config.LogrusHook, level logrus.Level, componentName string) { + syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, componentName) + if err == nil { + logrus.AddHook(&logLevelHook{level, syslogHook}) + } +} + //CloseAndLogIfError Closes io.Closer and logs the error if any func CloseAndLogIfError(ctx context.Context, closer io.Closer, message string) { if closer == nil {