Skip to content

Commit

Permalink
Allow caller code to use Colors directly without if (#40)
Browse files Browse the repository at this point in the history
* Allow caller code to use Colors directly without if and they'll get reset to empty string if color mode is disabled (and vice versa)

* linter fix + added test

* better doc
  • Loading branch information
ldemailly authored Aug 1, 2023
1 parent c0e607f commit a05b03a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ When on console:
![Example console color output](color.png)

JSON formatted logs can also be converted back to text later/after capture and similarly colorized using [fortio.org/logc](https://github.com/fortio/logc#logc)

The `log.Colors` can be used by callers and they'll be empty string when not in color mode, and the ansi escape codes otherwise.
16 changes: 15 additions & 1 deletion console_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ var (
// these should really be constants but go doesn't have constant structs, arrays etc...

// ANSI color codes.
Colors = color{
// This isn't meant to be used directly and is here only to document the names of the struct.
// Use the Colors variable instead.
ANSIColors = color{
Reset: "\033[0m",
Red: "\033[31m",
Green: "\033[32m",
Expand All @@ -55,6 +57,11 @@ var (
DarkGray: "\033[90m",
}

// ANSI color codes or empty depending on ColorMode.
// These will be reset to empty string if color is disabled (see ColorMode() and SetColorMode()).
// Start with actual colors, will be reset to empty if color is disabled.
Colors = ANSIColors

// Mapping of log levels to color.
LevelToColor = []string{
Colors.Gray,
Expand All @@ -81,8 +88,15 @@ func ConsoleLogging() bool {

// SetColorMode computes whether we currently should be using color text mode or not.
// Need to be reset if config changes (but is already automatically re evaluated when calling SetOutput()).
// It will reset the Colors variable to either be the actual escape sequences or empty strings (when
// color is disabled).
func SetColorMode() {
Color = ColorMode()
if Color {
Colors = ANSIColors
} else {
Colors = color{}
}
}

// ColorMode returns true if we should be using color text mode, which is either because it's
Expand Down
23 changes: 23 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,31 @@ func TestColorMode(t *testing.T) {
if cs == "" {
t.Errorf("got empty color timestamp")
}
if Colors.Green == "" {
t.Errorf("expected to have green color not empty when in color mode")
}
prevGreen := Colors.Green
// turn off color mode
Config.ForceColor = false
SetColorMode()
if Color {
t.Errorf("expected to not be in color mode after SetColorMode() and forcecolor false")
}
if Colors.Green != "" {
t.Errorf("expected to have green color empty when not color mode, got %q", Colors.Green)
}
// Show one can mutate/change/tweak colors
ANSIColors.Green = "foo"
Config.ForceColor = true
SetColorMode()
if Colors.Green != "foo" {
t.Errorf("expected to have green color preserved, got %q", Colors.Green)
}
// put it back to real green for other tests
ANSIColors.Green = prevGreen
// Reset for other/further tests
Config.ForceColor = false
SetColorMode()
}

func TestSetLevel(t *testing.T) {
Expand Down

0 comments on commit a05b03a

Please sign in to comment.