Repository: rs/zerolog
Pull request #597 (2023-10-13 13:35:13)

Make console output more readable
See https://github.com/rs/zerolog/pull/596#issuecomment-1761424984 for description of differences.


Patch:
diff --git a/console.go b/console.go
index 02104f4..917d749 100644
--- a/console.go
+++ b/console.go
@@ -281,7 +281,7 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
 		}
 	case MessageFieldName:
 		if w.FormatMessage == nil {
-			f = consoleDefaultFormatMessage
+			f = consoleDefaultFormatMessage(w.NoColor, evt[LevelFieldName])
 		} else {
 			f = w.FormatMessage
 		}
@@ -389,21 +389,21 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
 		if ll, ok := i.(string); ok {
 			switch ll {
 			case LevelTraceValue:
-				l = colorize("TRC", colorMagenta, noColor)
+				l = colorize("TRC", LevelColors["TRC"], noColor)
 			case LevelDebugValue:
-				l = colorize("DBG", colorYellow, noColor)
+				l = colorize("DBG", LevelColors["DBG"], noColor)
 			case LevelInfoValue:
-				l = colorize("INF", colorGreen, noColor)
+				l = colorize("INF", LevelColors["INF"], noColor)
 			case LevelWarnValue:
-				l = colorize("WRN", colorRed, noColor)
+				l = colorize("WRN", LevelColors["WRN"], noColor)
 			case LevelErrorValue:
-				l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
+				l = colorize("ERR", LevelColors["ERR"], noColor)
 			case LevelFatalValue:
-				l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
+				l = colorize("FTL", LevelColors["FTL"], noColor)
 			case LevelPanicValue:
-				l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
+				l = colorize("PNC", LevelColors["PNC"], noColor)
 			default:
-				l = colorize(ll, colorBold, noColor)
+				l = strings.ToUpper(ll)[0:3]
 			}
 		} else {
 			if i == nil {
@@ -434,11 +434,18 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
 	}
 }
 
-func consoleDefaultFormatMessage(i interface{}) string {
-	if i == nil {
-		return ""
+func consoleDefaultFormatMessage(noColor bool, level interface{}) Formatter {
+	return func(i interface{}) string {
+		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 {
@@ -459,6 +466,6 @@ func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
 
 func consoleDefaultFormatErrFieldValue(noColor bool) Formatter {
 	return func(i interface{}) string {
-		return colorize(fmt.Sprintf("%s", i), colorRed, noColor)
+		return colorize(colorize(fmt.Sprintf("%s", i), colorBold, noColor), colorRed, noColor)
 	}
 }
diff --git a/globals.go b/globals.go
index e1067de..7e5ca25 100644
--- a/globals.go
+++ b/globals.go
@@ -108,6 +108,18 @@ var (
 	// DefaultContextLogger is returned from Ctx() if there is no logger associated
 	// with the context.
 	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 (


Test patch:
diff --git a/console_test.go b/console_test.go
index bdb70a0..c3dbdb3 100644
--- a/console_test.go
+++ b/console_test.go
@@ -97,7 +97,7 @@ func TestConsoleWriter(t *testing.T) {
 			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()
 		if 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)
 		}
 
-		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()
 		if actualOutput != expectedOutput {
 			t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)