raythurman2386 / cognibot

Discord bot built with Pycord that offers AI chat and image generation inside of Discord. Image generations are saved in cloudinary and hosted online for all to download.
GNU General Public License v3.0
2 stars 1 forks source link

⚡️ Improved the structure of the Logger #46

Closed raythurman2386 closed 3 weeks ago

raythurman2386 commented 3 weeks ago

Description:

This PR refactors the existing logging implementation to enhance modularity, reusability, and performance for cognibot. The key improvements include:

  1. Encapsulation of Logging Logic:

    • Introduced a Logger class in logger.py that encapsulates the entire logging setup.
    • This class handles the creation and configuration of the logger, including setting up an AsyncRotatingFileHandler for non-blocking log writes.
  2. Asynchronous Log Handling:

    • Replaced the synchronous RotatingFileHandler with a custom AsyncRotatingFileHandler, which uses a ThreadPoolExecutor to asynchronously handle log writing. This prevents I/O operations from blocking the main application flow, improving performance.
  3. Signal Handling Integration:

    • Integrated signal handling within the logger.py file, ensuring that the logger gracefully shuts down when the application receives termination signals (e.g., SIGTERM, SIGINT).
    • This is managed through the setup_signal_handlers method in the Logger class.
  4. Simplified Import and Usage:

    • The logger instance (app_logger) is now set up in logger.py and can be easily imported into other files, maintaining a consistent logging setup across the application.
    • This change should not impact any existing logging code; importing app_logger in any file will give access to the pre-configured logger.

Changes Made:

Impact:

Testing: