Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify logging. Allow to passing handlers to entrypoint #220

Merged
merged 4 commits into from
Feb 10, 2025

Conversation

mosquito
Copy link
Collaborator

Summary

This PR introduces significant improvements to the logging system while also applying several style reformatting changes (referred to as “reformat with gray”) across the codebase.


Major Changes: Logging System Enhancements

  • New ThreadedHandler:

    • Introduces a dedicated logging handler that processes log records on a separate thread using an internal queue.
    • Improves asynchronous logging by offloading log processing from the event loop.
  • Deprecation of Legacy MemoryHandler Approach:

    • The previous mechanism (using logging.handlers.MemoryHandler and a background flusher) is replaced.
    • wrap_logging_handler now issues a deprecation warning and returns an instance of the new ThreadedHandler.
  • Updated Logging Configuration in basic_config:

    • User-provided handlers are now wrapped with ThreadedHandler, ensuring:
      • Proper asynchronous handling.
      • Integration with the unhandled exception hook.
      • Graceful shutdown via dedicated thread termination.
  • Additional Logging Adjustments:

    • Refined exception logging in UnhandledLoopHook for better trace formatting.
    • Exported the new ThreadedHandler in the public API to allow custom usage if needed.

Other Changes: Reformat with Gray

  • Function Signatures and Imports:

    • Added trailing commas to multi-line parameter lists for consistency.
    • Consolidated and reformatted import statements in several modules (e.g., aggregate.py, backoff.py, entrypoint.py, uvicorn.py, and timeout.py).
  • Minor Code Cleanups:

    • Updated syntax and formatting details to improve overall readability.
    • These changes are purely cosmetic and do not impact functionality.

Motivation

  • Enhanced Asynchronous Logging:
    By offloading logging to a separate thread, the new system minimizes the risk of blocking the asyncio event loop, especially under high load.

  • Improved Code Consistency:
    The reformatting changes (reformat with gray) make the codebase cleaner and more maintainable without altering any business logic.


Impact

  • The logging improvements are transparent for most users, aside from receiving a deprecation warning if using the old wrap_logging_handler.
  • The reformatting changes have no functional impact and only serve to standardize the code style.

Migration

  • For Users:

    • No immediate changes are required. However, users with custom logging configurations should be aware of the deprecation of the old memory handler mechanism.
    • It is recommended to transition to the new logging system to benefit from the improved performance and reliability.
  • Documentation:

    • Update any custom documentation or configuration guides to reflect the changes in logging setup and usage of ThreadedHandler.

Feedback and suggestions are welcome. This refactor sets a robust foundation for asynchronous logging while ensuring the code remains clean and consistent through the reformatting updates.

@mosquito mosquito merged commit 056e965 into master Feb 10, 2025
16 checks passed
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.

2 participants