Logging to a file

Command line argument -log-file will add a logging hook that writes to a
file in addition to the standard error stream.
This commit is contained in:
Emmanuel BENOîT 2021-02-11 23:30:36 +01:00
parent 31cb613822
commit 30cae773cf
2 changed files with 26 additions and 2 deletions

View file

@ -54,7 +54,6 @@ To Do
* Proper logging, work in progress:
* Sending logs to... well, Graylog... through CLI switches.
* Writing logs to a file.
* 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'

27
main.go
View file

@ -2,7 +2,10 @@ package main
import (
"flag"
"os"
"github.com/sirupsen/logrus"
lrh_wr "github.com/sirupsen/logrus/hooks/writer"
)
type (
@ -14,6 +17,8 @@ type (
instance string
// The log level.
logLevel string
// A file to write logs into.
logFile string
}
)
@ -27,7 +32,8 @@ func parseCommandLine() cliFlags {
flags := cliFlags{}
flag.StringVar(&flags.cfgFile, "c", "graylog-groups.yml", "Configuration file.")
flag.StringVar(&flags.instance, "i", "", "Instance identifier.")
flag.StringVar(&flags.logLevel, "L", "", "Log level for the logrus library.")
flag.StringVar(&flags.logLevel, "L", "", "Log level.")
flag.StringVar(&flags.logFile, "log-file", "", "Log file.")
flag.Parse()
return flags
}
@ -57,10 +63,29 @@ func toLogLevel(cliLevel string) logrus.Level {
return logrus.InfoLevel
}
// Add a file writer hook to the logging library.
func configureLogFile(path string) {
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err == nil {
logrus.AddHook(&lrh_wr.Hook{
Writer: file,
LogLevels: logrus.AllLevels,
})
} else {
log.WithFields(logrus.Fields{
"error": err,
"file": path,
}).Error("Could not open log file")
}
}
// Configure the logging library based on the various command line flags.
func configureLogging(flags cliFlags) {
log = getLoggingContext(flags.instance)
log.Logger.SetLevel(toLogLevel(flags.logLevel))
if flags.logFile != "" {
configureLogFile(flags.logFile)
}
}
func main() {