Logging to a Graylog server

Added the --log-graylog command line argument which sends logs to a
Graylog server using GELF/UDP.
This commit is contained in:
Emmanuel BENOîT 2021-02-13 23:42:32 +01:00
parent aac67dd277
commit f012a16f05
3 changed files with 12 additions and 6 deletions

View file

@ -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'

1
go.mod
View file

@ -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

15
main.go
View file

@ -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 <hostname>:<port>.
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: <host>:<port>)")
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)
}