From 03ba213b8a1ff6979efc110970ecb8594349b96f Mon Sep 17 00:00:00 2001 From: Rickard Dybeck Date: Tue, 16 Feb 2016 16:27:38 +0100 Subject: [PATCH] Make ParseLevel case-insensitive Coming from an environment where loglevels are always specified in uppercase, having ParseLevel be case-insensitive is a nice to have. --- logrus.go | 3 ++- logrus_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/logrus.go b/logrus.go index 0c09fbc..fa8a7cc 100644 --- a/logrus.go +++ b/logrus.go @@ -3,6 +3,7 @@ package logrus import ( "fmt" "log" + "strings" ) // Fields type, used to pass to `WithFields`. @@ -33,7 +34,7 @@ func (level Level) String() string { // ParseLevel takes a string level and returns the Logrus log level constant. func ParseLevel(lvl string) (Level, error) { - switch lvl { + switch strings.ToLower(lvl) { case "panic": return PanicLevel, nil case "fatal": diff --git a/logrus_test.go b/logrus_test.go index b7d9302..2cf03f2 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -255,30 +255,58 @@ func TestParseLevel(t *testing.T) { assert.Nil(t, err) assert.Equal(t, PanicLevel, l) + l, err = ParseLevel("PANIC") + assert.Nil(t, err) + assert.Equal(t, PanicLevel, l) + l, err = ParseLevel("fatal") assert.Nil(t, err) assert.Equal(t, FatalLevel, l) + l, err = ParseLevel("FATAL") + assert.Nil(t, err) + assert.Equal(t, FatalLevel, l) + l, err = ParseLevel("error") assert.Nil(t, err) assert.Equal(t, ErrorLevel, l) + l, err = ParseLevel("ERROR") + assert.Nil(t, err) + assert.Equal(t, ErrorLevel, l) + l, err = ParseLevel("warn") assert.Nil(t, err) assert.Equal(t, WarnLevel, l) + l, err = ParseLevel("WARN") + assert.Nil(t, err) + assert.Equal(t, WarnLevel, l) + l, err = ParseLevel("warning") assert.Nil(t, err) assert.Equal(t, WarnLevel, l) + l, err = ParseLevel("WARNING") + assert.Nil(t, err) + assert.Equal(t, WarnLevel, l) + l, err = ParseLevel("info") assert.Nil(t, err) assert.Equal(t, InfoLevel, l) + l, err = ParseLevel("INFO") + assert.Nil(t, err) + assert.Equal(t, InfoLevel, l) + l, err = ParseLevel("debug") assert.Nil(t, err) assert.Equal(t, DebugLevel, l) + l, err = ParseLevel("DEBUG") + assert.Nil(t, err) + assert.Equal(t, DebugLevel, l) + l, err = ParseLevel("invalid") assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error()) }