Skip to content

Fix #708 – Clear exc_info and stack_info before formatting in InterceptHandler#719

Open
Pranav1921 wants to merge 2 commits into
AOSSIE-Org:mainfrom
Pranav1921:issue-708
Open

Fix #708 – Clear exc_info and stack_info before formatting in InterceptHandler#719
Pranav1921 wants to merge 2 commits into
AOSSIE-Org:mainfrom
Pranav1921:issue-708

Conversation

@Pranav1921
Copy link
Copy Markdown

@Pranav1921 Pranav1921 commented Dec 12, 2025

This PR fixes a logging issue where exc_info and stack_info were preserved before formatting, causing unwanted tracebacks in logs. These fields are now cleared before rerouting logs.

Problem (Before Fix):
[APP] | ERROR | Division by zero occurred! Traceback (most recent call last): File "test_logging.py", line 8, in <module> 1 / 0 ZeroDivisionError: division by zero

Changes Made:

  • Cleared record.exc_info and record.stack_info before formatting in setup_logging.py.
  • Verified manually that rerouted logs no longer include tracebacks.

After Fix:
[APP] | ERROR | Division by zero occurred!

Testing:

  • Manual tests with ColorFormatter confirmed tracebacks are removed.
  • Normal info and error messages still work correctly.

Closes: #708

Summary by CodeRabbit

  • Bug Fixes
    • Adjusted logging formatting to suppress tracebacks in formatted output while preserving original exception and stack details for other logging handlers, resulting in cleaner logs without losing underlying error information.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 12, 2025

Walkthrough

ColorFormatter.format now preserves original record.exc_info and record.stack_info, clears them while performing formatting to suppress tracebacks, then restores the originals after formatting.

Changes

Cohort / File(s) Change Summary
ColorFormatter: logging exc/stack handling
backend/app/logging/setup_logging.py, sync-microservice/app/logging/setup_logging.py
Save original record.exc_info and record.stack_info, set them to None during formatting to prevent automatic traceback/stack output, then restore the originals after formatting. No public APIs changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Small, focused change applied to two parallel files.
  • Review attention:
    • Verify exc_info/stack_info restoration is correct even on exceptions during formatting.
    • Confirm no handlers rely on cleared values during the formatter's run.

Poem

🐰 In the burrow I tidy each log and nook,
I tuck away traces from every book.
Exc_info hushed, stacks gently restored,
Clean lines hop out — nothing ignored. ✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: clearing exc_info and stack_info in InterceptHandler to fix logging issue #708.
Linked Issues check ✅ Passed The PR successfully implements the required fix from issue #708 by clearing exc_info and stack_info before formatting in ColorFormatter, preventing unwanted tracebacks in logs.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing issue #708; modifications only address the exc_info and stack_info preservation issue in the logging setup without introducing unrelated changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3733b9c and 3e69215.

📒 Files selected for processing (2)
  • backend/app/logging/setup_logging.py (1 hunks)
  • sync-microservice/app/logging/setup_logging.py (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 548
File: backend/app/logging/setup_logging.py:224-243
Timestamp: 2025-10-13T16:40:22.622Z
Learning: In the PictoPy project's centralized logging system (backend/app/logging/setup_logging.py and sync-microservice/app/logging/setup_logging.py), stack traces should not be shown in logs. When rerouting logs through InterceptHandler, exc_info and stack_info should not be preserved.
📚 Learning: 2025-10-13T16:40:22.622Z
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 548
File: backend/app/logging/setup_logging.py:224-243
Timestamp: 2025-10-13T16:40:22.622Z
Learning: In the PictoPy project's centralized logging system (backend/app/logging/setup_logging.py and sync-microservice/app/logging/setup_logging.py), stack traces should not be shown in logs. When rerouting logs through InterceptHandler, exc_info and stack_info should not be preserved.

Applied to files:

  • sync-microservice/app/logging/setup_logging.py
  • backend/app/logging/setup_logging.py
📚 Learning: 2025-10-15T07:13:34.502Z
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 548
File: backend/app/database/images.py:115-115
Timestamp: 2025-10-15T07:13:34.502Z
Learning: In the PictoPy backend and sync-microservice, exception logging using `logger.error(f"...")` without stack traces (i.e., without `exc_info=True` or `logger.exception()`) is acceptable for console logging, as this is a deliberate design decision made with the maintainer for console-only logs.

Applied to files:

  • sync-microservice/app/logging/setup_logging.py
  • backend/app/logging/setup_logging.py
🧬 Code graph analysis (1)
backend/app/logging/setup_logging.py (1)
sync-microservice/app/logging/setup_logging.py (1)
  • format (63-122)
🔇 Additional comments (2)
sync-microservice/app/logging/setup_logging.py (1)

65-82: LGTM! The save/restore pattern correctly suppresses tracebacks while preserving original values.

This implementation properly addresses the issue by clearing exc_info and stack_info before formatting to prevent traceback output, then restoring the originals so other handlers remain unaffected. Based on learnings, this aligns with the project's deliberate design decision that stack traces should not be shown in logs.

backend/app/logging/setup_logging.py (1)

65-82: LGTM! Correctly addresses Issue #708 by clearing exc_info/stack_info before formatting.

The save/restore pattern ensures tracebacks are suppressed during this formatter's output while preserving the original values for any other handlers that might need them. Implementation is consistent with the sync-microservice version.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
backend/app/logging/setup_logging.py (1)

63-68: Avoid mutating LogRecord in-place; save/restore exc_info and stack_info.
Clearing these fields on the shared record can leak into other handlers/formatters if the record is processed elsewhere. Prefer temporarily clearing during formatting and restoring afterwards.

 def format(self, record: logging.LogRecord) -> str:
     """Format the log record with colors and component prefix."""
-    # Clear exc_info and stack_info before formatting
-    record.exc_info = None
-    record.stack_info = None
+    # Clear exc_info and stack_info before formatting (but don't mutate permanently)
+    _exc_info, _stack_info = record.exc_info, record.stack_info
+    record.exc_info = None
+    record.stack_info = None
+    try:
+        # Add component information to the record
+        component_prefix = self.component_config.get("prefix", "")
+        record.component = component_prefix
+
+        # Format the message
+        formatted_message = super().format(record)
+    finally:
+        record.exc_info, record.stack_info = _exc_info, _stack_info
-
-    # Add component information to the record
-    component_prefix = self.component_config.get("prefix", "")
-    record.component = component_prefix
-
-    # Format the message
-    formatted_message = super().format(record)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d07d817 and 3733b9c.

📒 Files selected for processing (1)
  • backend/app/logging/setup_logging.py (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 548
File: backend/app/logging/setup_logging.py:224-243
Timestamp: 2025-10-13T16:40:22.622Z
Learning: In the PictoPy project's centralized logging system (backend/app/logging/setup_logging.py and sync-microservice/app/logging/setup_logging.py), stack traces should not be shown in logs. When rerouting logs through InterceptHandler, exc_info and stack_info should not be preserved.
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 548
File: backend/app/database/images.py:115-115
Timestamp: 2025-10-15T07:13:34.502Z
Learning: In the PictoPy backend and sync-microservice, exception logging using `logger.error(f"...")` without stack traces (i.e., without `exc_info=True` or `logger.exception()`) is acceptable for console logging, as this is a deliberate design decision made with the maintainer for console-only logs.
📚 Learning: 2025-10-13T16:40:22.622Z
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 548
File: backend/app/logging/setup_logging.py:224-243
Timestamp: 2025-10-13T16:40:22.622Z
Learning: In the PictoPy project's centralized logging system (backend/app/logging/setup_logging.py and sync-microservice/app/logging/setup_logging.py), stack traces should not be shown in logs. When rerouting logs through InterceptHandler, exc_info and stack_info should not be preserved.

Applied to files:

  • backend/app/logging/setup_logging.py

Comment thread backend/app/logging/setup_logging.py
…restore pattern

- Add exc_info and stack_info clearing to sync-microservice ColorFormatter
- Use save/restore pattern in both backend and sync-microservice to avoid affecting other handlers
- Ensures consistent behavior across both components
- Addresses CodeRabbit review comment about incomplete fix
@github-actions github-actions Bot added backend bug Something isn't working labels Dec 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging: InterceptHandler preserves exc_info and stack_info (should be cleared before formatting)

1 participant