From 4482e62d2d3dd8cb4a56d439da6dbd68d5ceb399 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 21 Nov 2019 00:44:44 +0000 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20use=20log=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/setup.go | 16 +++----------- log/log.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/cli/setup.go b/cli/setup.go index a17a3d37..e89d3ec7 100644 --- a/cli/setup.go +++ b/cli/setup.go @@ -182,11 +182,6 @@ func setup() { var chk map[string]bool - // Setup a default logging - // hook for cli output - - logger := &log.DefaultHook{} - // Ensure that the specified // logging level is allowed @@ -206,7 +201,7 @@ func setup() { log.Fatal("Incorrect log level specified") } - logger.SetLevel(opts.Logging.Level) + log.SetLevel(opts.Logging.Level) } @@ -224,7 +219,7 @@ func setup() { log.Fatal("Incorrect log format specified") } - logger.SetFormat(opts.Logging.Format) + log.SetFormat(opts.Logging.Format) } @@ -243,15 +238,10 @@ func setup() { log.Fatal("Incorrect log output specified") } - logger.SetOutput(opts.Logging.Output) + log.SetOutput(opts.Logging.Output) } - // Add the default logging hook - // to the logger instance - - log.Hook(logger) - // Enable global options object cnf.Settings = opts diff --git a/log/log.go b/log/log.go index d048c3e5..1e6b3231 100644 --- a/log/log.go +++ b/log/log.go @@ -17,6 +17,8 @@ package log import ( "fmt" "io/ioutil" + "os" + "time" "github.com/sirupsen/logrus" ) @@ -99,11 +101,6 @@ func Instance() *logrus.Logger { return log.Logger } -// Hook adds a logging hook to the logger instance -func Hook(hook logrus.Hook) { - log.AddHook(hook) -} - func IsPanic() bool { return log.IsLevelEnabled(PanicLevel) } @@ -132,6 +129,60 @@ func IsTrace() bool { return log.IsLevelEnabled(TraceLevel) } +// SetLevel sets the logging level of the logger instance. +func SetLevel(v string) { + switch v { + case "trace": + log.Logger.SetLevel(TraceLevel) + case "debug": + log.Logger.SetLevel(DebugLevel) + case "info": + log.Logger.SetLevel(InfoLevel) + case "warn": + log.Logger.SetLevel(WarnLevel) + case "error": + log.Logger.SetLevel(ErrorLevel) + case "fatal": + log.Logger.SetLevel(FatalLevel) + case "panic": + log.Logger.SetLevel(PanicLevel) + } +} + +// SetOutput sets the logging output of the logger instance. +func SetOutput(v string) { + switch v { + case "none": + log.Logger.SetOutput(ioutil.Discard) + case "stdout": + log.Logger.SetOutput(os.Stdout) + case "stderr": + log.Logger.SetOutput(os.Stderr) + } +} + +// SetFormat sets the logging format of the logger instance. +func SetFormat(v string) { + switch v { + case "json": + log.Logger.SetFormatter(&JSONFormatter{ + IgnoreFields: []string{ + "ctx", + "vars", + }, + TimestampFormat: time.RFC3339, + }) + case "text": + log.Logger.SetFormatter(&TextFormatter{ + IgnoreFields: []string{ + "ctx", + "vars", + }, + TimestampFormat: time.RFC3339, + }) + } +} + func Display(v ...interface{}) { if isTerminal { fmt.Print(v...)