From b0e939c367393dbf769fdb07e931d3d96bcc84b7 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 30 May 2018 11:43:14 -0400 Subject: [PATCH] Allow users to set the log level for the root log15 handler. Using the GODOC_LOG_LEVEL environment variable, an administrator running this service can decide which logs to print. This is helpful for toggling off debugging logs which may contain sensitive information such as credentials. --- log/log.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/log/log.go b/log/log.go index 60d603b5..41a6cfa7 100644 --- a/log/log.go +++ b/log/log.go @@ -15,6 +15,25 @@ const ( loggerKey ) +func init() { + // Set the log level via the GODOC_LOG_LEVEL environment variable. + // Valid values can be found in the code for log15.LvlFromString, + // but include the standard "debug", "info", "warn", "error", and + // "crit" (critical). + // + // This only sets the Root logger on the log15 package. If the context + // contains a logger, then this environment variable will not affect + // log level. + // + // If the GODOC_LOG_LEVEL env var is blank, then we do nothing. + if logLevelStr := os.Getenv("GODOC_LOG_LEVEL"); logLevelStr != "" { + logLevel, _ := log15.LvlFromString(logLevelStr) + log15.Root().SetHandler( + log15.LvlFilterHandler(logLevel, log15.StdoutHandler), + ) + } +} + // FromContext always returns a logger. If there is no logger in the context, it // returns the root logger. It is not recommended for use and may be removed in // the future.