Skip to content

fix: strip ANSI colors from logged exceptions#155

Open
heavry wants to merge 3 commits into
Qix-:masterfrom
heavry:fix-log-no-color
Open

fix: strip ANSI colors from logged exceptions#155
heavry wants to merge 3 commits into
Qix-:masterfrom
heavry:fix-log-no-color

Conversation

@heavry

@heavry heavry commented May 17, 2026

Copy link
Copy Markdown

Closes #87

Summary

When exceptions are logged via Python's logging module, ANSI color escape codes appear as raw text in log files. This fix ensures:

  • stderr output: keeps colored exceptions (terminal experience preserved)
  • File/other handlers: exceptions are plain text without ANSI codes

Implementation

Modified better_exceptions/log.py to patch logging handlers differently based on type:

  • StreamHandler targeting sys.stderr → uses colored format_exception
  • All other handlers (FileHandler, etc.) → uses ExceptionFormatter(colored=False)

Changes

  • better_exceptions/log.py — handler-aware color stripping
  • test/test_file_logging.py — test verifying file output has no ANSI codes while stderr does

Acceptance criteria

  • Logged exceptions to files do not contain ANSI color codes
  • Terminal stderr output still shows colored exceptions
  • Existing functionality preserved
  • Test included

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