Skip to content

Commit

Permalink
Extends the string format of the log level.
Browse files Browse the repository at this point in the history
  • Loading branch information
edoger committed Dec 27, 2020
1 parent 5076af5 commit 3b16dee
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 13 deletions.
55 changes: 43 additions & 12 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,52 @@ const (
type Level uint32

// All supported log levels.
var allLevels = map[Level]string{
PanicLevel: "panic", FatalLevel: "fatal",
ErrorLevel: "error", WarnLevel: "warn", InfoLevel: "info",
DebugLevel: "debug", TraceLevel: "trace",
var allLevels = map[Level][]string{
PanicLevel: {"panic", "PANIC", "pnc", "PNC"},
FatalLevel: {"fatal", "FATAL", "fat", "FAT"},
ErrorLevel: {"error", "ERROR", "err", "ERR"},
WarnLevel: {"warn", "WARN", "wan", "WAN"},
InfoLevel: {"info", "INFO", "inf", "INF"},
DebugLevel: {"debug", "DEBUG", "dbg", "DBG"},
TraceLevel: {"trace", "TRACE", "tac", "TAC"},
}

// String returns the string form of the current level.
// If the log level is not supported, always returns "unknown".
func (level Level) String() string {
if s, found := allLevels[level]; found {
return s
return s[0]
}
return "unknown"
}

// CapitalString returns the capital string form of the current level.
// If the log level is not supported, always returns "UNKNOWN".
func (level Level) CapitalString() string {
if s, found := allLevels[level]; found {
return s[1]
}
return "UNKNOWN"
}

// ShortString returns the short string form of the current level.
// If the log level is not supported, always returns "uno".
func (level Level) ShortString() string {
if s, found := allLevels[level]; found {
return s[2]
}
return "uno"
}

// ShortCapitalString returns the short capital string form of the current level.
// If the log level is not supported, always returns "UNO".
func (level Level) ShortCapitalString() string {
if s, found := allLevels[level]; found {
return s[3]
}
return "UNO"
}

// IsValid determines whether the current level is valid.
func (level Level) IsValid() bool {
return level <= TraceLevel && level >= PanicLevel
Expand All @@ -78,19 +109,19 @@ func (level Level) IsEnabled(l Level) bool {
// ParseLevel parses the log level from the given string.
func ParseLevel(s string) (Level, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "panic":
case "panic", "pnc":
return PanicLevel, nil
case "fatal":
case "fatal", "fat":
return FatalLevel, nil
case "error":
case "error", "err":
return ErrorLevel, nil
case "warn", "warning":
case "warn", "wan", "warning":
return WarnLevel, nil
case "info":
case "info", "inf", "echo":
return InfoLevel, nil
case "debug":
case "debug", "dbg":
return DebugLevel, nil
case "trace":
case "trace", "tac", "print":
return TraceLevel, nil
}
// A level zero value is not a supported level.
Expand Down
77 changes: 76 additions & 1 deletion level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,72 @@ func TestLevel_String(t *testing.T) {
}
}

func TestLevel_CapitalString(t *testing.T) {
items := []struct {
Given Level
Want string
}{
{PanicLevel, "PANIC"},
{FatalLevel, "FATAL"},
{ErrorLevel, "ERROR"},
{WarnLevel, "WARN"},
{InfoLevel, "INFO"},
{DebugLevel, "DEBUG"},
{TraceLevel, "TRACE"},
{Level(0), "UNKNOWN"},
}

for _, item := range items {
if got := item.Given.CapitalString(); got != item.Want {
t.Fatalf("Level.CapitalString(): want %v, got %v", item.Want, got)
}
}
}

func TestLevel_ShortString(t *testing.T) {
items := []struct {
Given Level
Want string
}{
{PanicLevel, "pnc"},
{FatalLevel, "fat"},
{ErrorLevel, "err"},
{WarnLevel, "wan"},
{InfoLevel, "inf"},
{DebugLevel, "dbg"},
{TraceLevel, "tac"},
{Level(0), "uno"},
}

for _, item := range items {
if got := item.Given.ShortString(); got != item.Want {
t.Fatalf("Level.ShortString(): want %v, got %v", item.Want, got)
}
}
}

func TestLevel_ShortCapitalString(t *testing.T) {
items := []struct {
Given Level
Want string
}{
{PanicLevel, "PNC"},
{FatalLevel, "FAT"},
{ErrorLevel, "ERR"},
{WarnLevel, "WAN"},
{InfoLevel, "INF"},
{DebugLevel, "DBG"},
{TraceLevel, "TAC"},
{Level(0), "UNO"},
}

for _, item := range items {
if got := item.Given.ShortCapitalString(); got != item.Want {
t.Fatalf("Level.ShortCapitalString(): want %v, got %v", item.Want, got)
}
}
}

func TestLevel_IsValid(t *testing.T) {
items := []struct {
Given Level
Expand Down Expand Up @@ -92,14 +158,23 @@ func TestParseLevel(t *testing.T) {
Erred bool
}{
{"panic", PanicLevel, false},
{"pnc", PanicLevel, false},
{"fatal", FatalLevel, false},
{"fat", FatalLevel, false},
{"error", ErrorLevel, false},
{"err", ErrorLevel, false},
{"warn", WarnLevel, false},
{"wan", WarnLevel, false},
{"warning", WarnLevel, false},
{"info", InfoLevel, false},
{"inf", InfoLevel, false},
{"echo", InfoLevel, false},
{"debug", DebugLevel, false},
{"dbg", DebugLevel, false},
{"trace", TraceLevel, false},
{"unknown", Level(0), true},
{"tac", TraceLevel, false},
{"print", TraceLevel, false},
{"unknown", 0, true},
}

for _, item := range items {
Expand Down

0 comments on commit 3b16dee

Please sign in to comment.