Skip to content

feat: Unify logging and add request middleware#1442

Open
edwinjosechittilappilly wants to merge 3 commits intorelease-saas-0.1from
remove-log-points
Open

feat: Unify logging and add request middleware#1442
edwinjosechittilappilly wants to merge 3 commits intorelease-saas-0.1from
remove-log-points

Conversation

@edwinjosechittilappilly
Copy link
Copy Markdown
Collaborator

This pull request refactors exception handling and logging across the codebase to improve consistency, clarity, and structure. The main changes include replacing traceback.print_exc() and unstructured error logs with logger.exception() calls that include context-specific messages and, where appropriate, structured fields. Additionally, logging is now configured immediately on application startup to ensure all modules use the correct logger setup.

Logging and Exception Handling Improvements:

  • Replaced all instances of traceback.print_exc() and generic logger.error() calls with logger.exception() for better stack trace visibility and context-specific error messages throughout the API and connector modules. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]

  • Updated many log messages to include a clear context prefix (e.g., [AUTH], [CHAT], [CONNECTOR], [API], [CONFIG], [LF], [INGEST]) for easier log filtering and diagnosis. [1] [2] [3] [4] [5] [6] [7] [8]

Application Startup and Logging Configuration:

  • Refactored src/bootstrap.py to configure environment variables and structured logging as the very first step in application startup, ensuring all loggers are properly set up before any other imports.

Additional Logging Enhancements:

  • Improved log detail and structure for key events such as onboarding state updates, document processing, and authentication flows, including more granular log levels (e.g., logger.warning for recoverable issues). [1] [2] [3] [4] [5] [6]

Code Cleanup:

  • Removed unused code and main guards from connector modules and eliminated redundant imports.

These changes collectively improve the maintainability, observability, and reliability of the codebase by standardizing how errors and important events are logged.

Copilot AI review requested due to automatic review settings April 20, 2026 22:44
@github-actions github-actions bot added backend 🔷 Issues related to backend services (OpenSearch, Langflow, APIs) enhancement 🔵 New feature or request labels Apr 20, 2026
Copy link
Copy Markdown
Contributor

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 standardizes structured logging across the API/services/connectors, moves logging configuration to the earliest possible point in startup, and adds an ASGI request middleware for correlation IDs + single-line request summaries.

Changes:

  • Replaced traceback.print_exc() / ad-hoc error logging with logger.exception() and consistent context prefixes.
  • Refactored logging configuration (structlog + stdlib bridge) and moved initial configuration into bootstrap to run before other imports.
  • Added a pure-ASGI RequestLoggingMiddleware that binds request_id/method/path and emits a single [API] Request log with duration + status.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/utils/logging_config.py Expands structlog setup with shared processors, adds stdlib logging bridge, adds header sanitization helper and filtering processors.
src/bootstrap.py Loads .env and immediately configures logging on import to ensure consistent logger setup early in startup.
src/main.py Removes explicit configure_from_env() call, adds ASGI request logging middleware, and prevents uvicorn from overriding logging config.
src/dependencies.py Updates auth-related log prefixes (notably IBM auth paths).
src/utils/opensearch_utils.py Normalizes OpenSearch security/setup logging prefixes and adjusts shutdown logging.
src/utils/acl_utils.py Replaces print(...) error paths with structured logging.
src/services/search_service.py Adjusts search-related log levels and adds [SEARCH] prefixes for key events.
src/services/langflow_file_service.py Removes logging of headers/payload; adds ingestion start/complete logs.
src/services/chat_service.py Removes verbose header logging; adds structured [CHAT] request/response logs.
src/services/auth_service.py Replaces traceback printing with logger.exception for better tracebacks.
src/api/upload.py Adds structured logging for ingestion upload failures and access-denied cases.
src/api/settings.py Improves onboarding logs (structured fields, warning vs info).
src/api/provider_validation.py Promotes a fallback attempt log to warning with [API] prefix.
src/api/knowledge_filter.py Converts webhook failure logging to logger.exception with context fields.
src/api/connectors.py Converts webhook failure logging to logger.exception with context fields; adjusts error levels.
src/api/chat.py Converts error paths to logger.exception and adds missing error logs in history endpoints.
src/api/auth.py Adds module logger and replaces traceback printing with structured exceptions.
src/connectors/service.py Promotes connector document processing logs to structured [CONNECTOR] info events.
src/connectors/sharepoint/oauth.py Replaces traceback printing with logger.exception across SharePoint OAuth flows.
src/connectors/sharepoint/connector.py Replaces traceback printing with logger.exception; improves detect-url warning structure.
src/connectors/onedrive/oauth.py Replaces traceback printing with logger.exception across OneDrive OAuth flows.
src/connectors/onedrive/connector.py Replaces traceback printing with logger.exception; improves detect-url warning structure.
src/connectors/ibm_cos/connector.py Removes __main__ guard/test code from connector module.
src/config/settings.py Adjusts log level for forced key regeneration to warning.
src/config/config_manager.py Reduces config logging payload volume; adds [CONFIG] prefix.
src/agent.py Replaces traceback printing with logger.exception in agent streaming/non-streaming paths.

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

Comment thread src/utils/logging_config.py
Comment thread src/main.py Outdated
Comment thread src/utils/acl_utils.py Outdated
Comment thread src/utils/acl_utils.py Outdated
Comment thread src/main.py
Comment thread src/dependencies.py Outdated
Comment thread src/utils/logging_config.py Outdated
Replace ad-hoc prints/tracebacks with structured logger.exception/logger.* calls across the codebase and standardize log messages/levels (agent, api, connectors, services, utils). Introduce a redesigned utils/logging_config.py with structured logging processors, JSON/dev renderers, stdlib logging bridge, header sanitization, and environment-driven configuration. Add a pure-ASGI RequestLoggingMiddleware in main.py to bind request_id and emit per-request structured logs (status, duration) and inject x-request-id. Misc: update log contexts, remove test __main__ in IBM COS connector, and improve a few log levels/messages for clarity and observability.
Configure structured logging immediately after loading .env so module-level loggers are correct on import (bootstrap.py). Move get_logger after configuration and emit a startup info log. Remove redundant configure_from_env() from main and prevent Uvicorn from overriding structlog by passing log_config=None to uvicorn.run. Enhance logging_config: add json_logs flag, choose JSON vs console renderer, route uvicorn loggers through our handler, suppress noisy third‑party libraries, capture Python warnings via logging, and tune logger levels. Lowered Opensearch shutdown noise by changing a warning to a debug message in opensearch_utils.
@github-actions github-actions bot added enhancement 🔵 New feature or request and removed enhancement 🔵 New feature or request labels Apr 20, 2026
Adjust logging and error handling across several modules:

- src/dependencies.py: Remove a development debug log that printed IBM auth headers.
- src/main.py: Stop importing configure_from_env (only get_logger kept). Ensure a single x-request-id in responses by removing any upstream header values before injecting the generated correlation ID.
- src/utils/acl_utils.py: Replace logger.error calls with logger.exception to include tracebacks when ACL checks or updates fail.
- src/utils/logging_config.py: Change filter_health_and_metrics to only suppress routine access-log INFO entries for high-frequency endpoints (errors/warnings still surface). Also use dict_tracebacks when JSON logs are enabled to improve traceback formatting.

These changes reduce noisy logs, ensure consistent request ID headers, and provide better diagnostic information for ACL-related errors.
@github-actions github-actions bot added enhancement 🔵 New feature or request and removed enhancement 🔵 New feature or request labels Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend 🔷 Issues related to backend services (OpenSearch, Langflow, APIs) enhancement 🔵 New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants