mirror of
https://github.com/rs/zerolog
synced 2026-06-08 17:13:30 +00:00
Make console output more readable. (#597)
This commit is contained in:
+21
-14
@@ -281,7 +281,7 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
|
|||||||
}
|
}
|
||||||
case MessageFieldName:
|
case MessageFieldName:
|
||||||
if w.FormatMessage == nil {
|
if w.FormatMessage == nil {
|
||||||
f = consoleDefaultFormatMessage
|
f = consoleDefaultFormatMessage(w.NoColor, evt[LevelFieldName])
|
||||||
} else {
|
} else {
|
||||||
f = w.FormatMessage
|
f = w.FormatMessage
|
||||||
}
|
}
|
||||||
@@ -389,21 +389,21 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
|
|||||||
if ll, ok := i.(string); ok {
|
if ll, ok := i.(string); ok {
|
||||||
switch ll {
|
switch ll {
|
||||||
case LevelTraceValue:
|
case LevelTraceValue:
|
||||||
l = colorize("TRC", colorMagenta, noColor)
|
l = colorize("TRC", LevelColors["TRC"], noColor)
|
||||||
case LevelDebugValue:
|
case LevelDebugValue:
|
||||||
l = colorize("DBG", colorYellow, noColor)
|
l = colorize("DBG", LevelColors["DBG"], noColor)
|
||||||
case LevelInfoValue:
|
case LevelInfoValue:
|
||||||
l = colorize("INF", colorGreen, noColor)
|
l = colorize("INF", LevelColors["INF"], noColor)
|
||||||
case LevelWarnValue:
|
case LevelWarnValue:
|
||||||
l = colorize("WRN", colorRed, noColor)
|
l = colorize("WRN", LevelColors["WRN"], noColor)
|
||||||
case LevelErrorValue:
|
case LevelErrorValue:
|
||||||
l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
|
l = colorize("ERR", LevelColors["ERR"], noColor)
|
||||||
case LevelFatalValue:
|
case LevelFatalValue:
|
||||||
l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
|
l = colorize("FTL", LevelColors["FTL"], noColor)
|
||||||
case LevelPanicValue:
|
case LevelPanicValue:
|
||||||
l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
|
l = colorize("PNC", LevelColors["PNC"], noColor)
|
||||||
default:
|
default:
|
||||||
l = colorize(ll, colorBold, noColor)
|
l = strings.ToUpper(ll)[0:3]
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if i == nil {
|
if i == nil {
|
||||||
@@ -434,11 +434,18 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func consoleDefaultFormatMessage(i interface{}) string {
|
func consoleDefaultFormatMessage(noColor bool, level interface{}) Formatter {
|
||||||
if i == nil {
|
return func(i interface{}) string {
|
||||||
return ""
|
if i == nil || i == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
switch level {
|
||||||
|
case LevelInfoValue, LevelWarnValue, LevelErrorValue, LevelFatalValue, LevelPanicValue:
|
||||||
|
return colorize(fmt.Sprintf("%s", i), colorBold, noColor)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%s", i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s", i)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func consoleDefaultFormatFieldName(noColor bool) Formatter {
|
func consoleDefaultFormatFieldName(noColor bool) Formatter {
|
||||||
@@ -459,6 +466,6 @@ func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
|
|||||||
|
|
||||||
func consoleDefaultFormatErrFieldValue(noColor bool) Formatter {
|
func consoleDefaultFormatErrFieldValue(noColor bool) Formatter {
|
||||||
return func(i interface{}) string {
|
return func(i interface{}) string {
|
||||||
return colorize(fmt.Sprintf("%s", i), colorRed, noColor)
|
return colorize(colorize(fmt.Sprintf("%s", i), colorBold, noColor), colorRed, noColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -97,7 +97,7 @@ func TestConsoleWriter(t *testing.T) {
|
|||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
|
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[33mWRN\x1b[0m \x1b[1mFoobar\x1b[0m\n"
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
@@ -248,7 +248,7 @@ func TestConsoleWriter(t *testing.T) {
|
|||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar \x1b[36mfoo=\x1b[0mbar\n"
|
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[33mWRN\x1b[0m \x1b[1mFoobar\x1b[0m \x1b[36mfoo=\x1b[0mbar\n"
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
|
|||||||
+12
@@ -108,6 +108,18 @@ var (
|
|||||||
// DefaultContextLogger is returned from Ctx() if there is no logger associated
|
// DefaultContextLogger is returned from Ctx() if there is no logger associated
|
||||||
// with the context.
|
// with the context.
|
||||||
DefaultContextLogger *Logger
|
DefaultContextLogger *Logger
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 109 KiB After Width: | Height: | Size: 116 KiB |
Reference in New Issue
Block a user