From e7d713380c6952b4e51260e4770605e8f84a8d1b Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet <juan@benet.ai> Date: Fri, 17 Oct 2014 00:51:57 -0700 Subject: [PATCH] colored logfmt --- util/log.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/util/log.go b/util/log.go index 6a66024de..d373229ad 100644 --- a/util/log.go +++ b/util/log.go @@ -13,8 +13,19 @@ func init() { var log = Logger("util") -// LogFormat is the format used for our logger. -var LogFormat = "%{color}%{time:2006-01-02 15:04:05.999999} %{shortfile} %{level}: %{color:reset}%{message}" +var ansiGray = "\033[0;37m" + +// LogFormats is a map of formats used for our logger, keyed by name. +var LogFormats = map[string]string{ + "default": "%{color}%{time:2006-01-02 15:04:05.999999} %{level} %{shortfile}: %{color:reset}%{message}", + "color": ansiGray + "%{time:15:04:05.999} %{color}%{level} " + ansiGray + "%{shortfile}: %{color:reset}%{message}", +} + +// Logging environment variables +const ( + envLogging = "IPFS_LOGGING" + envLoggingFmt = "IPFS_LOGGING_FMT" +) // loggers is the set of loggers in the system var loggers = map[string]*logging.Logger{} @@ -26,13 +37,19 @@ func POut(format string, a ...interface{}) { // SetupLogging will initialize the logger backend and set the flags. func SetupLogging() { + + fmt := LogFormats[os.Getenv(envLoggingFmt)] + if fmt == "" { + fmt = LogFormats["default"] + } + backend := logging.NewLogBackend(os.Stderr, "", 0) logging.SetBackend(backend) - logging.SetFormatter(logging.MustStringFormatter(LogFormat)) + logging.SetFormatter(logging.MustStringFormatter(fmt)) lvl := logging.ERROR - if logenv := os.Getenv("IPFS_LOGGING"); logenv != "" { + if logenv := os.Getenv(envLogging); logenv != "" { var err error lvl, err = logging.LogLevel(logenv) if err != nil { -- GitLab