Don’t use log hooks
This commit is contained in:
parent
536493ef45
commit
4482e62d2d
2 changed files with 59 additions and 18 deletions
16
cli/setup.go
16
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
|
||||
|
|
61
log/log.go
61
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...)
|
||||
|
|
Loading…
Reference in a new issue