Honor NO_COLOR in the terminal log handler (closes #402)
check / check (push) Successful in 15m48s
check / check (push) Successful in 15m48s
The TTY log handler always wrote ANSI color, unlike internal/ui, which disables color when NO_COLOR is set. Add a color flag to TTYHandler, set from NO_COLOR with the same test internal/ui uses, and route every color code through a paint helper that returns plain text when color is off. The README's color sentence now covers log lines too. Model: deepseek-v4-pro
This commit is contained in:
1 parent
5abacbc5c1
commit
eff0763d0a
3 files changed
+50
-11
No files matched your search
@@ -421,3 +421,19 @@ func TestTTYHandlerEmptyGroupAndAttrsAreNoOps(t *testing.T) {
|
||||
assert.Contains(t, plain, "kept=yes")
|
||||
assert.NotContains(t, plain, " =")
|
||||
}
|
||||
|
||||
// TestTTYHandlerRespectsNoColor checks that the handler writes no ANSI
|
||||
// color when NO_COLOR is set, matching internal/ui's shouldColor test.
|
||||
// The handler is only ever chosen for a terminal, so this is the one
|
||||
// remaining condition that can still leave color in log output.
|
||||
func TestTTYHandlerRespectsNoColor(t *testing.T) {
|
||||
t.Setenv("NO_COLOR", "1")
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
logger := slog.New(log.NewTTYHandler(&buf, debugHandlerOptions()))
|
||||
logger.Info("hello", "key", "value")
|
||||
|
||||
assert.NotContains(t, buf.String(), "\x1b[",
|
||||
"NO_COLOR must suppress every ANSI escape in log output")
|
||||
}
|
||||
Reference in new issue
Block a user