Skip to content

middleware: honour NoColor in defaultLogEntry.Panic#1086

Open
alliasgher wants to merge 2 commits intogo-chi:masterfrom
alliasgher:fix-logger-panic-nocolor
Open

middleware: honour NoColor in defaultLogEntry.Panic#1086
alliasgher wants to merge 2 commits intogo-chi:masterfrom
alliasgher:fix-logger-panic-nocolor

Conversation

@alliasgher
Copy link
Copy Markdown

Summary

defaultLogEntry.Panic called PrintPrettyStack unconditionally. That function hardcodes useColor := true, so the panic stack trace was always printed with ANSI colour codes even when DefaultLogFormatter.NoColor was set to true.

Fix

  • Add PrintPrettyStackColor(rvr interface{}, useColor bool) that accepts an explicit colour flag.
  • Refactor PrintPrettyStack to delegate to it with useColor=true — no change for existing callers.
  • Refactor prettyStack.parse to receive useColor rather than hardcode it.
  • defaultLogEntry.Panic now passes l.useColor, which is derived from DefaultLogFormatter.NoColor.

Fixes #1042

When a Tee writer is set, ReadFrom calls io.Copy(&f.basicWriter, r),
which routes each write through basicWriter.Write. Write already
increments basicWriter.bytes, so the line

    f.basicWriter.bytes += int(n)

that followed the io.Copy double-counted every byte, causing
BytesWritten() to return twice the actual value.

Remove the redundant addition. The non-tee path is unaffected because
it delegates to the underlying ResponseWriter's ReaderFrom (bypassing
Write) and must still add n itself.

Fixes go-chi#1067

Signed-off-by: alliasgher <alliasgher123@gmail.com>
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 go-chi#1042

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

middleware.Logger: defaultLogEntry.Panic() does not respect 'NoColor' of DefaultLogFormatter

1 participant