Skip to content

Keep file logging tracebacks uncolored#149

Open
dicnunz wants to merge 2 commits into
Qix-:masterfrom
dicnunz:fix-logging-file-colors
Open

Keep file logging tracebacks uncolored#149
dicnunz wants to merge 2 commits into
Qix-:masterfrom
dicnunz:fix-logging-file-colors

Conversation

@dicnunz
Copy link
Copy Markdown

@dicnunz dicnunz commented May 9, 2026

Fixes #87.

IssueHunt bounty: https://issuehunt.io/r/Qix-/better-exceptions/issues/87

This is a fresh, tested replacement for the stale direction in #99. That PR is still dirty/stale and had requested changes; this keeps the scope limited to the logging color behavior.

Reproduction

When a logger has both a stderr StreamHandler and a FileHandler, logging formats the same LogRecord for each handler. If the stream handler formats first, logging caches record.exc_text with ANSI color codes, and the file handler can reuse that colored cached traceback.

Changes

  • Adds a colored= parameter to better_exceptions.format_exception().
  • Patches each logging handler formatter with the right exception formatter: stderr stream handlers keep color, file handlers use uncolored traceback formatting.
  • Clears and restores record.exc_text while formatting exceptions so each handler recomputes traceback text with its own color policy.
  • Adds a regression test that asserts stream output remains colored while file output has no ANSI escapes.

Tests

Passed:

  • FORCE_COLOR=1 TERM=xterm PYTHONPATH=. python3 test/test_file_logging.py
  • python3 -m py_compile better_exceptions/init.py better_exceptions/log.py test/test_file_logging.py
  • Manual handler-order smoke test: stream-first and file-first both produced file_ansi=False and stream_ansi=True.

Not fully completed locally:

  • PYTHON=python3 ./test_all.sh: this machine does not have the repo's expected python command available by default, and under the available Python 3.14 the existing golden-output normalizer does not normalize truncated module reprs. The new regression test above is direct and passing.

@dicnunz
Copy link
Copy Markdown
Author

dicnunz commented May 13, 2026

Pushed ba53553 with two merge-readiness fixes: shared Formatter instances are now cloned per handler, and AssertionError.args is restored after each logging formatter pass so colored assertion text cannot leak between handlers. I also moved the silent regression into assert_case so the full golden-output suite continues after it.

Verified:

  • FORCE_COLOR=1 TERM=xterm PYTHONPATH=. python3 test/test_file_logging.py
  • python3 -m py_compile better_exceptions/__init__.py better_exceptions/log.py test/test_file_logging.py
  • from a short /tmp/be149 copy with a python -> python3 shim: PYTHON=python3 BETEXC_PYTHON=python ./test_all.sh

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.

Logged exceptions shouldn't contain colour

1 participant