Merge pull request #326 from alde/make-parselevel-case-insensitive

Make ParseLevel case-insensitive
This commit is contained in:
Simon Eskildsen 2016-02-18 11:43:37 -05:00
commit 840e99181e
2 changed files with 30 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package logrus
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
) )
// Fields type, used to pass to `WithFields`. // 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. // ParseLevel takes a string level and returns the Logrus log level constant.
func ParseLevel(lvl string) (Level, error) { func ParseLevel(lvl string) (Level, error) {
switch lvl { switch strings.ToLower(lvl) {
case "panic": case "panic":
return PanicLevel, nil return PanicLevel, nil
case "fatal": case "fatal":

View File

@ -255,30 +255,58 @@ func TestParseLevel(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, PanicLevel, l) assert.Equal(t, PanicLevel, l)
l, err = ParseLevel("PANIC")
assert.Nil(t, err)
assert.Equal(t, PanicLevel, l)
l, err = ParseLevel("fatal") l, err = ParseLevel("fatal")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, FatalLevel, l) assert.Equal(t, FatalLevel, l)
l, err = ParseLevel("FATAL")
assert.Nil(t, err)
assert.Equal(t, FatalLevel, l)
l, err = ParseLevel("error") l, err = ParseLevel("error")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, ErrorLevel, l) assert.Equal(t, ErrorLevel, l)
l, err = ParseLevel("ERROR")
assert.Nil(t, err)
assert.Equal(t, ErrorLevel, l)
l, err = ParseLevel("warn") l, err = ParseLevel("warn")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, WarnLevel, l) assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("WARN")
assert.Nil(t, err)
assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("warning") l, err = ParseLevel("warning")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, WarnLevel, l) assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("WARNING")
assert.Nil(t, err)
assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("info") l, err = ParseLevel("info")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, InfoLevel, l) assert.Equal(t, InfoLevel, l)
l, err = ParseLevel("INFO")
assert.Nil(t, err)
assert.Equal(t, InfoLevel, l)
l, err = ParseLevel("debug") l, err = ParseLevel("debug")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, DebugLevel, l) assert.Equal(t, DebugLevel, l)
l, err = ParseLevel("DEBUG")
assert.Nil(t, err)
assert.Equal(t, DebugLevel, l)
l, err = ParseLevel("invalid") l, err = ParseLevel("invalid")
assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error()) assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error())
} }