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:
parent
31cb613822
commit
30cae773cf
2 changed files with 26 additions and 2 deletions
|
@ -54,7 +54,6 @@ To Do
|
||||||
|
|
||||||
* Proper logging, work in progress:
|
* Proper logging, work in progress:
|
||||||
* Sending logs to... well, Graylog... through CLI switches.
|
* Sending logs to... well, Graylog... through CLI switches.
|
||||||
* Writing logs to a file.
|
|
||||||
* Document command line flags.
|
* Document command line flags.
|
||||||
* Add TLS options (skip checks / specify CA) for the Graylog API.
|
* Add TLS options (skip checks / specify CA) for the Graylog API.
|
||||||
* Read object ownership using `grn_permissions` to preserve privileges on users'
|
* Read object ownership using `grn_permissions` to preserve privileges on users'
|
||||||
|
|
27
main.go
27
main.go
|
@ -2,7 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
lrh_wr "github.com/sirupsen/logrus/hooks/writer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -14,6 +17,8 @@ type (
|
||||||
instance string
|
instance string
|
||||||
// The log level.
|
// The log level.
|
||||||
logLevel string
|
logLevel string
|
||||||
|
// A file to write logs into.
|
||||||
|
logFile string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,7 +32,8 @@ func parseCommandLine() cliFlags {
|
||||||
flags := cliFlags{}
|
flags := cliFlags{}
|
||||||
flag.StringVar(&flags.cfgFile, "c", "graylog-groups.yml", "Configuration file.")
|
flag.StringVar(&flags.cfgFile, "c", "graylog-groups.yml", "Configuration file.")
|
||||||
flag.StringVar(&flags.instance, "i", "", "Instance identifier.")
|
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()
|
flag.Parse()
|
||||||
return flags
|
return flags
|
||||||
}
|
}
|
||||||
|
@ -57,10 +63,29 @@ func toLogLevel(cliLevel string) logrus.Level {
|
||||||
return logrus.InfoLevel
|
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.
|
// Configure the logging library based on the various command line flags.
|
||||||
func configureLogging(flags cliFlags) {
|
func configureLogging(flags cliFlags) {
|
||||||
log = getLoggingContext(flags.instance)
|
log = getLoggingContext(flags.instance)
|
||||||
log.Logger.SetLevel(toLogLevel(flags.logLevel))
|
log.Logger.SetLevel(toLogLevel(flags.logLevel))
|
||||||
|
if flags.logFile != "" {
|
||||||
|
configureLogFile(flags.logFile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in a new issue