Skip to content

Commit 0a7f297

Browse files
committed
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]: https://github.com/sirupsen/logrus/blob/dd1b4c2e81afc5c255f216a722b012ed26be57df/exported.go#L34C1-L37 [2]: https://github.com/sirupsen/logrus/blob/dd1b4c2e81afc5c255f216a722b012ed26be57df/exported.go#L39-L42 [3]: https://github.com/sirupsen/logrus/blob/dd1b4c2e81afc5c255f216a722b012ed26be57df/exported.go#L23-L26 [4]: https://github.com/sirupsen/logrus/blob/dd1b4c2e81afc5c255f216a722b012ed26be57df/exported.go#L9-L16 Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 85a2c9a) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 9d175a1 commit 0a7f297

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

log/context.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import (
4545
)
4646

4747
// L is an alias for the standard logger.
48-
var L = logrus.NewEntry(logrus.StandardLogger())
48+
var L = &Entry{
49+
Logger: logrus.StandardLogger(),
50+
// Default is three fields plus a little extra room.
51+
Data: make(Fields, 6),
52+
}
4953

5054
type loggerKey struct{}
5155

@@ -116,13 +120,13 @@ func SetLevel(level string) error {
116120
return err
117121
}
118122

119-
logrus.SetLevel(lvl)
123+
L.Logger.SetLevel(lvl)
120124
return nil
121125
}
122126

123127
// GetLevel returns the current log level.
124128
func GetLevel() Level {
125-
return logrus.GetLevel()
129+
return L.Logger.GetLevel()
126130
}
127131

128132
// OutputFormat specifies a log output format.
@@ -141,13 +145,13 @@ const (
141145
func SetFormat(format OutputFormat) error {
142146
switch format {
143147
case TextFormat:
144-
logrus.SetFormatter(&logrus.TextFormatter{
148+
L.Logger.SetFormatter(&logrus.TextFormatter{
145149
TimestampFormat: RFC3339NanoFixed,
146150
FullTimestamp: true,
147151
})
148152
return nil
149153
case JSONFormat:
150-
logrus.SetFormatter(&logrus.JSONFormatter{
154+
L.Logger.SetFormatter(&logrus.JSONFormatter{
151155
TimestampFormat: RFC3339NanoFixed,
152156
})
153157
return nil

0 commit comments

Comments
 (0)