[PR #425] Unify backend logging, add context keys, and capture uncaught exceptions #730

Open
opened 2026-03-13 21:06:09 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/425
Author: @AJaySi
Created: 3/12/2026
Status: 🔄 Open

Base: mainHead: codex/enhance-logging-configuration-and-startup-flow


📝 Commits (1)

  • e77bde4 Unify backend logging and capture uncaught exceptions

📊 Changes

2 files changed (+124 additions, -14 deletions)

View changed files

📝 backend/logging_config.py (+122 -13)
📝 backend/start_alwrity_backend.py (+2 -1)

📄 Description

Motivation

  • Ensure all runtime errors (sync and async) are captured and logged with full tracebacks to improve observability and post-mortem debugging.
  • Provide a consistent, minimally-verbose default logging output while allowing verbose developer mode to show full diagnostics.
  • Route Uvicorn logs into the same sinks/format so startup/server logs match application logs and respect the chosen verbosity.

Description

  • Added global uncaught exception handling by registering sys.excepthook and an asyncio loop exception handler to surface unhandled exceptions with tracebacks via Loguru in backend/logging_config.py.
  • Introduced default structured context keys (request_id, job_id, user_id) via logger.configure(patcher=...) and included them in a shared common_format so logger.bind(...) context appears consistently.
  • Implemented an InterceptHandler that routes standard logging records into Loguru and wired uvicorn, uvicorn.error, and uvicorn.access to use this handler so they share the same sinks/format in both verbose and non-verbose modes.
  • Preserved concise non-verbose behavior (warning+), but enabled backtrace/diagnose and included {exception} in the format so ERROR/CRITICAL logs that include exceptions still print full tracebacks.
  • Updated startup to call uvicorn.run(..., log_config=None) in backend/start_alwrity_backend.py so Uvicorn does not override the unified logging configuration.

Testing

  • Ran python -m compileall backend/logging_config.py backend/start_alwrity_backend.py to validate syntax and byte-compile both modified modules, which completed successfully.
  • Confirmed local file diffs show the intended changes to backend/logging_config.py and backend/start_alwrity_backend.py and that the startup invocation includes log_config=None (no runtime tests were run as part of this change).

Codex Task


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/425 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/12/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `codex/enhance-logging-configuration-and-startup-flow` --- ### 📝 Commits (1) - [`e77bde4`](https://github.com/AJaySi/ALwrity/commit/e77bde4cebb7974f6ef36fce01525918d811c74a) Unify backend logging and capture uncaught exceptions ### 📊 Changes **2 files changed** (+124 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `backend/logging_config.py` (+122 -13) 📝 `backend/start_alwrity_backend.py` (+2 -1) </details> ### 📄 Description ### Motivation - Ensure all runtime errors (sync and async) are captured and logged with full tracebacks to improve observability and post-mortem debugging. - Provide a consistent, minimally-verbose default logging output while allowing verbose developer mode to show full diagnostics. - Route Uvicorn logs into the same sinks/format so startup/server logs match application logs and respect the chosen verbosity. ### Description - Added global uncaught exception handling by registering `sys.excepthook` and an asyncio loop exception handler to surface unhandled exceptions with tracebacks via Loguru in `backend/logging_config.py`. - Introduced default structured context keys (`request_id`, `job_id`, `user_id`) via `logger.configure(patcher=...)` and included them in a shared `common_format` so `logger.bind(...)` context appears consistently. - Implemented an `InterceptHandler` that routes standard `logging` records into Loguru and wired `uvicorn`, `uvicorn.error`, and `uvicorn.access` to use this handler so they share the same sinks/format in both verbose and non-verbose modes. - Preserved concise non-verbose behavior (warning+), but enabled backtrace/diagnose and included `{exception}` in the format so ERROR/CRITICAL logs that include exceptions still print full tracebacks. - Updated startup to call `uvicorn.run(..., log_config=None)` in `backend/start_alwrity_backend.py` so Uvicorn does not override the unified logging configuration. ### Testing - Ran `python -m compileall backend/logging_config.py backend/start_alwrity_backend.py` to validate syntax and byte-compile both modified modules, which completed successfully. - Confirmed local file diffs show the intended changes to `backend/logging_config.py` and `backend/start_alwrity_backend.py` and that the startup invocation includes `log_config=None` (no runtime tests were run as part of this change). ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69b1468c19fc83288b784fa601da9f54) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#730
No description provided.