Merge pull request #208 from Arkan/master

Fix Entry log level
This commit is contained in:
Simon Eskildsen 2015-07-07 05:29:58 -04:00
commit 7eac5879a5
3 changed files with 36 additions and 20 deletions

View File

@ -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
logrus/core: add support for DragonflyBSD and NetBSD

View File

@ -33,6 +33,7 @@ func NewEntry(logger *Logger) *Entry {
Logger: logger,
// Default is three fields, give a little extra room
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...))
}
}

View File

@ -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")
}