From e956af079214a9d66d81d4bcf986a024147f11e0 Mon Sep 17 00:00:00 2001 From: frrist Date: Tue, 11 Sep 2018 16:30:36 -0700 Subject: [PATCH] add env for logging to file (fix #22) and syslog --- oldlog.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/oldlog.go b/oldlog.go index 33ce430..aef4e18 100644 --- a/oldlog.go +++ b/oldlog.go @@ -31,8 +31,13 @@ var defaultLogFormat = "color" // Logging environment variables const ( + // TODO these env names should be more general, IPFS is not the only project to + // use go-log envLogging = "IPFS_LOGGING" envLoggingFmt = "IPFS_LOGGING_FMT" + + envLoggingFile = "GOLOG_FILE" // /path/to/file + envLoggingSyslog = "GOLOG_SYSLOG_ENABLE" // 1 or 0 ) // ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger @@ -43,15 +48,40 @@ var loggerMutex sync.RWMutex var loggers = map[string]*logging.Logger{} // SetupLogging will initialize the logger backend and set the flags. +// TODO calling this in `init` pushes all configuration to env variables +// - move it out of `init`? then we need to change all the code (js-ipfs, go-ipfs) to call this explicitly +// - have it look for a config file? need to define what that is func SetupLogging() { + // colorful or plain lfmt := LogFormats[os.Getenv(envLoggingFmt)] if lfmt == "" { lfmt = LogFormats[defaultLogFormat] } - backend := logging.NewLogBackend(colorable.NewColorableStderr(), "", 0) - logging.SetBackend(backend) + // check if we log to a file, or syslog, building a list of log backends + var lgbe []logging.Backend + if logfp := os.Getenv(envLoggingFile); len(logfp) > 0 { + f, err := os.Create(logfp) + if err != nil { + fmt.Printf("ERROR go-log: %s: failed to set logging file backend\n", err) + } + lgbe = append(lgbe, logging.NewLogBackend(f, "", 0)) + } + + if env := os.Getenv(envLoggingSyslog); env == "1" { + slbe, err := logging.NewSyslogBackend("") + if err != nil { + fmt.Printf("ERROR go-log: %s: failed to set logging syslog backend", err) + } + lgbe = append(lgbe, slbe) + } + + // logs written to stderr + lgbe = append(lgbe, logging.NewLogBackend(colorable.NewColorableStderr(), "", 0)) + + // set the backend(s) + logging.SetBackend(lgbe...) logging.SetFormatter(logging.MustStringFormatter(lfmt)) lvl := logging.ERROR