From f012a16f05e87318153d83a5291ba39810c0ba83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20BENO=C3=8ET?= Date: Sat, 13 Feb 2021 23:42:32 +0100 Subject: [PATCH] Logging to a Graylog server Added the --log-graylog command line argument which sends logs to a Graylog server using GELF/UDP. --- README.md | 2 -- go.mod | 1 + main.go | 15 +++++++++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3a05c4a..acc0f80 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,6 @@ go build To Do ------ -* Proper logging, work in progress: - * Sending logs to... well, Graylog... through CLI switches. * Document command line flags. * Add TLS options (skip checks / specify CA) for the Graylog API. * Read object ownership using `grn_permissions` to preserve privileges on users' diff --git a/go.mod b/go.mod index f6a18dd..b305b94 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module glgroups go 1.15 require ( + github.com/gemnasium/logrus-graylog-hook/v3 v3.0.2 github.com/go-ldap/ldap v3.0.3+incompatible github.com/sirupsen/logrus v1.7.0 gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect diff --git a/main.go b/main.go index 92d4714..e5dda18 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" + lrh_gl "github.com/gemnasium/logrus-graylog-hook/v3" "github.com/sirupsen/logrus" lrh_wr "github.com/sirupsen/logrus/hooks/writer" ) @@ -22,6 +23,8 @@ type ( logLevel string // A file to write logs into. logFile string + // Graylog server to send logs to (using GELF/UDP). Format is :. + logGraylog string } ) @@ -34,10 +37,11 @@ var ( func parseCommandLine() cliFlags { flags := cliFlags{} flag.StringVar(&flags.cfgFile, "c", "graylog-groups.yml", "Configuration file.") - flag.StringVar(&flags.instance, "i", "", "Instance identifier.") - flag.BoolVar(&flags.quiet, "q", false, "Quiet mode.") - flag.StringVar(&flags.logLevel, "L", "", "Log level.") - flag.StringVar(&flags.logFile, "log-file", "", "Log file.") + flag.StringVar(&flags.instance, "i", "", "Instance identifier") + flag.BoolVar(&flags.quiet, "q", false, "Quiet mode") + flag.StringVar(&flags.logLevel, "L", "", "Log level") + flag.StringVar(&flags.logFile, "log-file", "", "Log file") + flag.StringVar(&flags.logGraylog, "log-graylog", "", "Log to Graylog server (format: :)") flag.Parse() return flags } @@ -94,6 +98,9 @@ func configureLogging(flags cliFlags) { if flags.logFile != "" { configureLogFile(flags.logFile) } + if flags.logGraylog != "" { + log.Logger.AddHook(lrh_gl.NewGraylogHook(flags.logGraylog, nil)) + } if flags.quiet { log.Logger.SetOutput(ioutil.Discard) }