Skip to content

Commit

Permalink
add env for logging to file (fix #22) and syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed Sep 12, 2018
1 parent f2bdd6b commit 29c2a5b
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions oldlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,15 +48,43 @@ 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.Fprintf(os.Stderr, "ERROR go-log: %s: failed to set logging file backend\n", err)
} else {
lgbe = append(lgbe, logging.NewLogBackend(f, "", 0))
}
}

// check if we want a syslogger
if env := os.Getenv(envLoggingSyslog); env == "1" {
slbe, err := logging.NewSyslogBackend("")
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR go-log: %s: failed to set logging syslog backend", err)
} else {
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
Expand Down

2 comments on commit 29c2a5b

@DanielSel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the build on windows, since the logging library excludes NewSyslogBackend from the build under windows. Should we extract the syslog section into separate files for windows and other platforms with conditional compile rules?

@frrist
Copy link
Member Author

@frrist frrist commented on 29c2a5b Sep 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for finding this, I have addressed the issue here - #47

Please sign in to comment.