Skip to content

[MNT] Replace traceback.print_exc() with logging.exception() for structured logging#740

Open
aryan-2255 wants to merge 1 commit intoaiondemand:developfrom
aryan-2255:fix/replace-traceback-print-exc-with-logging
Open

[MNT] Replace traceback.print_exc() with logging.exception() for structured logging#740
aryan-2255 wants to merge 1 commit intoaiondemand:developfrom
aryan-2255:fix/replace-traceback-print-exc-with-logging

Conversation

@aryan-2255
Copy link
Copy Markdown

Replace all traceback.print_exc() calls with logging.exception() in production code. print_exc() writes to stderr which can be lost in Docker containers, while logging.exception() goes through the configured logging pipeline (Logstash, Grafana) and includes the full traceback automatically.

Files changed:

  • src/error_handling/error_handling.py
  • src/routers/resource_router.py
  • src/routers/resource_routers/platform_router.py

Change(s)

Change Type: Fixed

Change Category: Internal

Changelog Entry: Replace traceback.print_exc() with logging.exception() in 3 production files for proper structured logging.

Currently, when an unexpected exception with no args hits _raise_clean_http_exception() or as_http_exception(), the traceback is printed to stderr via print_exc(). In Docker, stderr output can be lost and is not captured by the Logstash shipper. logging.exception() produces the same full traceback but routes it through the Python logging system, making errors visible in Grafana.

How to Test

Run pytest src/tests/ and verify all tests pass. No special setup needed since logging.exception() is a drop-in replacement that produces the same traceback output.

Checklist

  • Tests have been added or updated to reflect the changes, or their absence is explicitly explained. No new tests needed since logging.exception() is a direct replacement with identical traceback output. All 855 existing tests pass.
  • Documentation has been added or updated to reflect the changes, or their absence is explicitly explained. No docs needed, this is an internal logging change.
  • A self-review has been conducted checking:
    • No unintended changes have been committed.
    • The changes in isolation seem reasonable.
  • All CI checks pass before pinging a reviewer, or provide an explanation if they do not.
  • The PR title matches the changelog entry's one-line description.

Related Issues

Closes #739

…ctured logging

Replace all traceback.print_exc() calls with logging.exception() in production
code. print_exc() writes to stderr which can be lost in Docker containers,
while logging.exception() goes through the configured logging pipeline
(Logstash → Grafana) and includes the full traceback automatically.

Files changed:
- src/error_handling/error_handling.py
- src/routers/resource_router.py
- src/routers/resource_routers/platform_router.py
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves production observability by routing unexpected exception tracebacks through the configured Python logging pipeline (instead of stderr) so they are captured by centralized logging/monitoring (e.g., Logstash/Grafana).

Changes:

  • Replaced traceback.print_exc() with logging.exception(...) in three production locations to preserve full tracebacks in structured logs.
  • Removed traceback imports where they are no longer used and added logging imports where needed.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/error_handling/error_handling.py Logs unexpected exceptions via logging.exception inside as_http_exception instead of printing to stderr.
src/routers/resource_router.py Uses logging.exception when an unexpected exception has no args during resource operations.
src/routers/resource_routers/platform_router.py Uses logging.exception when an unexpected exception has no args during platform operations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

[MNT] Replace traceback.print_exc() with logging.exception() for structured logging

2 participants