From 0be4ee004c3823c06f4d4386ca136c82ec1aa69b Mon Sep 17 00:00:00 2001 From: Arkan Date: Mon, 6 Jul 2015 22:27:39 +0200 Subject: [PATCH] Fix Entry log level --- CHANGELOG.md | 1 + entry.go | 41 +++++++++++++++++++++-------------------- entry_test.go | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a387154..ca482d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # (Unreleased) +logrus: fix entry log level (#208) logrus/core: improve performance of text formatter by 40% logrus/core: expose `LevelHooks` type diff --git a/entry.go b/entry.go index 699ea03..2a98065 100644 --- a/entry.go +++ b/entry.go @@ -32,7 +32,8 @@ func NewEntry(logger *Logger) *Entry { return &Entry{ Logger: logger, // Default is three fields, give a little extra room - Data: make(Fields, 5), + Data: make(Fields, 5), + Level: logger.Level, } } @@ -67,7 +68,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry { for k, v := range fields { data[k] = v } - return &Entry{Logger: entry.Logger, Data: data} + return &Entry{Logger: entry.Logger, Data: data, Level: entry.Level} } func (entry *Entry) log(level Level, msg string) { @@ -105,7 +106,7 @@ func (entry *Entry) log(level Level, msg string) { } func (entry *Entry) Debug(args ...interface{}) { - if entry.Logger.Level >= DebugLevel { + if entry.Level >= DebugLevel { entry.log(DebugLevel, fmt.Sprint(args...)) } } @@ -115,13 +116,13 @@ func (entry *Entry) Print(args ...interface{}) { } func (entry *Entry) Info(args ...interface{}) { - if entry.Logger.Level >= InfoLevel { + if entry.Level >= InfoLevel { entry.log(InfoLevel, fmt.Sprint(args...)) } } func (entry *Entry) Warn(args ...interface{}) { - if entry.Logger.Level >= WarnLevel { + if entry.Level >= WarnLevel { entry.log(WarnLevel, fmt.Sprint(args...)) } } @@ -131,20 +132,20 @@ func (entry *Entry) Warning(args ...interface{}) { } func (entry *Entry) Error(args ...interface{}) { - if entry.Logger.Level >= ErrorLevel { + if entry.Level >= ErrorLevel { entry.log(ErrorLevel, fmt.Sprint(args...)) } } func (entry *Entry) Fatal(args ...interface{}) { - if entry.Logger.Level >= FatalLevel { + if entry.Level >= FatalLevel { entry.log(FatalLevel, fmt.Sprint(args...)) } os.Exit(1) } func (entry *Entry) Panic(args ...interface{}) { - if entry.Logger.Level >= PanicLevel { + if entry.Level >= PanicLevel { entry.log(PanicLevel, fmt.Sprint(args...)) } panic(fmt.Sprint(args...)) @@ -153,13 +154,13 @@ func (entry *Entry) Panic(args ...interface{}) { // Entry Printf family functions func (entry *Entry) Debugf(format string, args ...interface{}) { - if entry.Logger.Level >= DebugLevel { + if entry.Level >= DebugLevel { entry.Debug(fmt.Sprintf(format, args...)) } } func (entry *Entry) Infof(format string, args ...interface{}) { - if entry.Logger.Level >= InfoLevel { + if entry.Level >= InfoLevel { entry.Info(fmt.Sprintf(format, args...)) } } @@ -169,7 +170,7 @@ func (entry *Entry) Printf(format string, args ...interface{}) { } func (entry *Entry) Warnf(format string, args ...interface{}) { - if entry.Logger.Level >= WarnLevel { + if entry.Level >= WarnLevel { entry.Warn(fmt.Sprintf(format, args...)) } } @@ -179,20 +180,20 @@ func (entry *Entry) Warningf(format string, args ...interface{}) { } func (entry *Entry) Errorf(format string, args ...interface{}) { - if entry.Logger.Level >= ErrorLevel { + if entry.Level >= ErrorLevel { entry.Error(fmt.Sprintf(format, args...)) } } func (entry *Entry) Fatalf(format string, args ...interface{}) { - if entry.Logger.Level >= FatalLevel { + if entry.Level >= FatalLevel { entry.Fatal(fmt.Sprintf(format, args...)) } os.Exit(1) } func (entry *Entry) Panicf(format string, args ...interface{}) { - if entry.Logger.Level >= PanicLevel { + if entry.Level >= PanicLevel { entry.Panic(fmt.Sprintf(format, args...)) } } @@ -200,13 +201,13 @@ func (entry *Entry) Panicf(format string, args ...interface{}) { // Entry Println family functions func (entry *Entry) Debugln(args ...interface{}) { - if entry.Logger.Level >= DebugLevel { + if entry.Level >= DebugLevel { entry.Debug(entry.sprintlnn(args...)) } } func (entry *Entry) Infoln(args ...interface{}) { - if entry.Logger.Level >= InfoLevel { + if entry.Level >= InfoLevel { entry.Info(entry.sprintlnn(args...)) } } @@ -216,7 +217,7 @@ func (entry *Entry) Println(args ...interface{}) { } func (entry *Entry) Warnln(args ...interface{}) { - if entry.Logger.Level >= WarnLevel { + if entry.Level >= WarnLevel { entry.Warn(entry.sprintlnn(args...)) } } @@ -226,20 +227,20 @@ func (entry *Entry) Warningln(args ...interface{}) { } func (entry *Entry) Errorln(args ...interface{}) { - if entry.Logger.Level >= ErrorLevel { + if entry.Level >= ErrorLevel { entry.Error(entry.sprintlnn(args...)) } } func (entry *Entry) Fatalln(args ...interface{}) { - if entry.Logger.Level >= FatalLevel { + if entry.Level >= FatalLevel { entry.Fatal(entry.sprintlnn(args...)) } os.Exit(1) } func (entry *Entry) Panicln(args ...interface{}) { - if entry.Logger.Level >= PanicLevel { + if entry.Level >= PanicLevel { entry.Panic(entry.sprintlnn(args...)) } } diff --git a/entry_test.go b/entry_test.go index 98717df..f7de400 100644 --- a/entry_test.go +++ b/entry_test.go @@ -51,3 +51,17 @@ func TestEntryPanicf(t *testing.T) { entry := NewEntry(logger) entry.WithField("err", errBoom).Panicf("kaboom %v", true) } + +func TestEntryLogLevel(t *testing.T) { + out := &bytes.Buffer{} + logger := New() + logger.Out = out + logger.Level = DebugLevel + entry := NewEntry(logger) + assert.Equal(t, DebugLevel, entry.Level) + entry.Level = WarnLevel + entry.Info("it should not be displayed") + assert.Equal(t, "", out.String()) + entry.Warn("it should be displayed") + assert.Contains(t, out.String(), "it should be displayed") +}