Ensure ColorFormatter restores original levelname for sibling handlers#2309
Ensure ColorFormatter restores original levelname for sibling handlers#2309KristopherKubicki wants to merge 1 commit intostagingfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| colored_handler.close() | ||
| plain_handler.close() | ||
| logger.propagate = original_propagate | ||
|
|
||
| colored_output = colored_stream.getvalue().strip() |
There was a problem hiding this comment.
Read StringIO after closing underlying stream
The regression test closes both handlers, but StreamHandler.close() also closes the attached StringIO. When execution reaches colored_stream.getvalue().strip() the stream is already closed and Python raises ValueError: I/O operation on closed file, so the test never reaches its assertions. Fetch the buffered contents before calling close() (or avoid closing the handlers) so the test exercises the behavior it is meant to verify.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where ColorFormatter would permanently modify the levelname attribute of LogRecord objects, causing sibling handlers (that share the same LogRecord instance) to incorrectly receive colorized level names even when using plain formatters.
Key Changes:
- Modified
ColorFormatter.format()to save, modify, and restore the originallevelnameusing a try/finally block - Added a regression test verifying that plain handlers remain unaffected when paired with colored handlers on the same logger
- Added a conditional check to only modify
levelnamewhen a color mapping exists
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| app/utils/logging_utils.py | Implements levelname preservation in ColorFormatter using try/finally to restore the original value after formatting |
| tests/test_color_formatter.py | Adds regression test verifying plain handlers stay uncolored when used alongside colored handlers |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Stale pull request message |
Summary
Testing
Codex Task