From 507a426bf81a37ca51485b39c2e1b2c179cb07e7 Mon Sep 17 00:00:00 2001 From: Mitar Date: Fri, 20 Oct 2023 07:17:01 -0700 Subject: [PATCH] Use map for formatted levels. (#599) --- console.go | 23 ++++++----------------- globals.go | 28 ++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/console.go b/console.go index 921e4b33..e8eeaa33 100644 --- a/console.go +++ b/console.go @@ -387,27 +387,16 @@ func consoleDefaultFormatLevel(noColor bool) Formatter { return func(i interface{}) string { var l string if ll, ok := i.(string); ok { - switch ll { - case LevelTraceValue: - l = colorize("TRC", LevelColors["TRC"], noColor) - case LevelDebugValue: - l = colorize("DBG", LevelColors["DBG"], noColor) - case LevelInfoValue: - l = colorize("INF", LevelColors["INF"], noColor) - case LevelWarnValue: - l = colorize("WRN", LevelColors["WRN"], noColor) - case LevelErrorValue: - l = colorize("ERR", LevelColors["ERR"], noColor) - case LevelFatalValue: - l = colorize("FTL", LevelColors["FTL"], noColor) - case LevelPanicValue: - l = colorize("PNC", LevelColors["PNC"], noColor) - default: + level, _ := ParseLevel(ll) + fl, ok := FormattedLevels[level] + if ok { + l = colorize(fl, LevelColors[level], noColor) + } else { l = strings.ToUpper(ll)[0:3] } } else { if i == nil { - l = colorize("???", colorBold, noColor) + l = "???" } else { l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3] } diff --git a/globals.go b/globals.go index 7e5ca25f..bf9e3da9 100644 --- a/globals.go +++ b/globals.go @@ -111,14 +111,26 @@ var ( // LevelColors are used by ConsoleWriter's consoleDefaultFormatLevel to color // log levels. - LevelColors = map[string]int{ - "TRC": colorBlue, - "DBG": 0, - "INF": colorGreen, - "WRN": colorYellow, - "ERR": colorRed, - "FTL": colorRed, - "PNC": colorRed, + LevelColors = map[Level]int{ + TraceLevel: colorBlue, + DebugLevel: 0, + InfoLevel: colorGreen, + WarnLevel: colorYellow, + ErrorLevel: colorRed, + FatalLevel: colorRed, + PanicLevel: colorRed, + } + + // FormattedLevels are used by ConsoleWriter's consoleDefaultFormatLevel + // for a short level name. + FormattedLevels = map[Level]string{ + TraceLevel: "TRC", + DebugLevel: "DBG", + InfoLevel: "INF", + WarnLevel: "WRN", + ErrorLevel: "ERR", + FatalLevel: "FTL", + PanicLevel: "PNC", } )