middleware: document printPrettyStack and harden NoColor panic test (#1131)

* middleware: honour NoColor in defaultLogEntry.Panic

defaultLogEntry.Panic called PrintPrettyStack unconditionally, which
always used ANSI colour codes regardless of the DefaultLogFormatter's
NoColor field.

Add PrintPrettyStackColor(rvr, useColor bool) and thread the flag
through prettyStack.parse. PrintPrettyStack now delegates to it with
useColor=true so existing callers are unaffected. defaultLogEntry.Panic
passes l.useColor so the panic output respects the same flag that
controls the rest of the request log.

Fixes #1042

Signed-off-by: alliasgher <alliasgher123@gmail.com>

* middleware: keep color-aware stack printer unexported

Address review: only defaultLogEntry.Panic needs the useColor variant
and it lives in the same package, so PrintPrettyStackColor does not need
to be exported. Rename it to printPrettyStack; the public PrintPrettyStack
API is unchanged.

Signed-off-by: alliasgher <alliasgher123@gmail.com>

* middleware: harden TestRecovererNoColor to actually guard the regression

Force IsTTY=true so cW would emit ANSI codes if useColor were true.
Without it the assertion is a no-op on non-TTY runners (e.g. CI) and
passes even against the buggy code path. Supersedes #1061.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Co-authored-by: alliasgher <alliasgher123@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vojtech Vitek (golang.cz) 2026-07-05 17:55:42 +02:00 committed by GitHub
parent 878fe71fc9
commit 7fcb8a2036
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View file

@ -55,6 +55,9 @@ func PrintPrettyStack(rvr interface{}) {
printPrettyStack(rvr, true)
}
// printPrettyStack prints a formatted stack trace to stderr. When useColor is
// false, ANSI colour codes are suppressed, which is useful for terminals that
// do not support them (e.g. on Windows) or when output is being captured.
func printPrettyStack(rvr interface{}, useColor bool) {
debugStack := debug.Stack()
s := prettyStack{}

View file

@ -46,6 +46,13 @@ func TestRecoverer(t *testing.T) {
}
func TestRecovererNoColor(t *testing.T) {
// Force IsTTY so cW would emit color codes if useColor were true; this is
// what makes the assertion below a real regression guard rather than a
// no-op on non-TTY test runners (e.g. CI).
oldIsTTY := IsTTY
IsTTY = true
defer func() { IsTTY = oldIsTTY }()
oldRecovererErrorWriter := recovererErrorWriter
defer func() { recovererErrorWriter = oldRecovererErrorWriter }()
buf := &bytes.Buffer{}