From 3d0f11976a6d6cb725fa0db623b4bc3fd6f4c19f Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 1 Aug 2023 18:18:41 -0700 Subject: [PATCH] fix tests and change color output to be more verbose/human readable --- logger.go | 12 +++++++++++- logger_test.go | 14 +++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/logger.go b/logger.go index 98c3eed..4ce8ab1 100644 --- a/logger.go +++ b/logger.go @@ -110,6 +110,16 @@ var ( "\"crit\"", "\"fatal\"", } + // Used for color version of console logging. + LevelToText = []string{ + "Debg", + "Verb", + "Info", + "Warn", + "Err!", + "Crit", + "Fatal", + } // Reverse mapping of level string used in JSON to Level. Used by https://github.com/fortio/logc // to interpret and colorize pre existing JSON logs. JSONStringLevelToLevel map[string]Level @@ -523,7 +533,7 @@ func Attr[T ValueTypes](key string, value T) KeyVal { } func colorLevelToStr(lvl Level) string { - return LevelToColor[lvl] + LevelToStrA[lvl][0:1] + Colors.DarkGray + return LevelToColor[lvl] + LevelToText[lvl] + Colors.DarkGray } // S logs a message of the given level with additional attributes. diff --git a/logger_test.go b/logger_test.go index d8496ae..9df2f81 100644 --- a/logger_test.go +++ b/logger_test.go @@ -59,7 +59,7 @@ func TestLoggerFilenameLine(t *testing.T) { expected := "D logger_test.go:51-prefix-test\n" + "E logger_test.go:53-prefix-SetLogLevel called with level -1 lower than Debug!\n" + "I logger_test.go:54-prefix-Log level is now 3 Warning (was 0 Debug)\n" + - "I -prefix-Should show despite being Info - unconditional Printf without line/file\n" + "I-prefix-Should show despite being Info - unconditional Printf without line/file\n" if actual != expected { t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected) } @@ -111,7 +111,7 @@ func Test_LogS_JSON_no_json_with_filename(t *testing.T) { _ = w.Flush() actual := b.String() expected := "W logger_test.go:109-bar-This will show, key1=\"value 1\", key2=\"42\"\n" + - "I -bar-This will show too\n" + "I-bar-This will show too\n" if actual != expected { t.Errorf("got %q expected %q", actual, expected) } @@ -145,10 +145,10 @@ func TestColorMode(t *testing.T) { actual := b.String() grID := fmt.Sprintf("[%d] ", goroutine.ID()) expected := "\x1b[37m" + grID + - "\x1b[33mW logger_test.go:138> With file and line\x1b[0m, \x1b[34mattr\x1b[0m=\x1b[33m\"value with space\"\x1b[0m\n" + - "\x1b[37m" + grID + "\x1b[32mI logger_test.go:139> info with file and line = true\x1b[0m\n" + - "\x1b[33mW > Without file and line\x1b[0m, \x1b[34mattr\x1b[0m=\x1b[33m\"value with space\"\x1b[0m\n" + - "\x1b[32mI > info with file and line = false\x1b[0m\n" + "\x1b[33mWarn\x1b[90m logger_test.go:138> \x1b[33mWith file and line\x1b[0m, \x1b[34mattr\x1b[0m=\x1b[33m\"value with space\"\x1b[0m\n" + + "\x1b[37m" + grID + "\x1b[32mInfo\x1b[90m logger_test.go:139> \x1b[32minfo with file and line = true\x1b[0m\n" + + "\x1b[33mWarn\x1b[90m> \x1b[33mWithout file and line\x1b[0m, \x1b[34mattr\x1b[0m=\x1b[33m\"value with space\"\x1b[0m\n" + + "\x1b[32mInfo\x1b[90m> \x1b[32minfo with file and line = false\x1b[0m\n" if actual != expected { t.Errorf("got:\n%q\nexpected:\n%q", actual, expected) } @@ -422,7 +422,7 @@ func Test_LogS_JSON_no_json_no_file(t *testing.T) { S(Warning, "This will show", Str("key1", "value 1"), Attr("key2", 42)) _ = w.Flush() actual := b.String() - expected := "W -foo-This will show, key1=\"value 1\", key2=\"42\"\n" + expected := "W-foo-This will show, key1=\"value 1\", key2=\"42\"\n" if actual != expected { t.Errorf("got %q expected %q", actual, expected) }