log: swap logrus functions with their equivalent on default logger
[`logrus.SetLevel()`][1], [`logrus.GetLevel()`][2] and [`logrus.SetFormatter()`][3] are all convenience functions to configure logrus' standardlogger, which is the logger to which we hold a reference in the Entry configured on [`log.L`][4]. This patch: - swaps calls to `logrus.SetLevel`, `logrus.GetLevel` and `logrus.SetFormatter` for their equivalents on `log.L`. This makes it clearer what `SetLevel` does, and makes sure that we set the log-level of the logger / entry we define in our package (even if that would be swapped with a different instance). - removes the use of `logrus.NewEntry` with directly constructing a `Entry`, using the local `Entry` alias (anticipating we can swap that type in future). [1]:dd1b4c2e81/exported.go (L34C1-L37)
[2]:dd1b4c2e81/exported.go (L39-L42)
[3]:dd1b4c2e81/exported.go (L23-L26)
[4]:dd1b4c2e81/exported.go (L9-L16)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
6baff1694f
commit
85a2c9a01b
@ -45,7 +45,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// L is an alias for the standard logger.
|
// L is an alias for the standard logger.
|
||||||
var L = logrus.NewEntry(logrus.StandardLogger())
|
var L = &Entry{
|
||||||
|
Logger: logrus.StandardLogger(),
|
||||||
|
// Default is three fields plus a little extra room.
|
||||||
|
Data: make(Fields, 6),
|
||||||
|
}
|
||||||
|
|
||||||
type loggerKey struct{}
|
type loggerKey struct{}
|
||||||
|
|
||||||
@ -116,13 +120,13 @@ func SetLevel(level string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.SetLevel(lvl)
|
L.Logger.SetLevel(lvl)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLevel returns the current log level.
|
// GetLevel returns the current log level.
|
||||||
func GetLevel() Level {
|
func GetLevel() Level {
|
||||||
return logrus.GetLevel()
|
return L.Logger.GetLevel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// OutputFormat specifies a log output format.
|
// OutputFormat specifies a log output format.
|
||||||
@ -141,13 +145,13 @@ const (
|
|||||||
func SetFormat(format OutputFormat) error {
|
func SetFormat(format OutputFormat) error {
|
||||||
switch format {
|
switch format {
|
||||||
case TextFormat:
|
case TextFormat:
|
||||||
logrus.SetFormatter(&logrus.TextFormatter{
|
L.Logger.SetFormatter(&logrus.TextFormatter{
|
||||||
TimestampFormat: RFC3339NanoFixed,
|
TimestampFormat: RFC3339NanoFixed,
|
||||||
FullTimestamp: true,
|
FullTimestamp: true,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
case JSONFormat:
|
case JSONFormat:
|
||||||
logrus.SetFormatter(&logrus.JSONFormatter{
|
L.Logger.SetFormatter(&logrus.JSONFormatter{
|
||||||
TimestampFormat: RFC3339NanoFixed,
|
TimestampFormat: RFC3339NanoFixed,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user