Skip to content

Commit

Permalink
Use map for formatted levels. (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar authored Oct 20, 2023
1 parent 8834256 commit 507a426
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
23 changes: 6 additions & 17 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
28 changes: 20 additions & 8 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
)

Expand Down

0 comments on commit 507a426

Please sign in to comment.