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.
This PR refactors the existing logging implementation to enhance modularity, reusability, and performance for cognibot. The key improvements include:
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.
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.
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.
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:
Added AsyncRotatingFileHandler class for asynchronous log handling.
Encapsulated logger setup within a new Logger class.
Added signal handling for clean shutdown of the logger.
Updated the structure to allow easy import and use of the app_logger across the codebase.
Impact:
Performance: The new async handler reduces the risk of I/O blocking, especially under heavy logging.
Code Maintenance: Centralized logger setup simplifies maintenance and future enhancements.
Backward Compatibility: Existing logging usage remains unaffected, with only minor adjustments needed to import the app_logger from logger.py.
Testing:
Verified that logs are written asynchronously and rotated as expected.
Confirmed that the logger shuts down gracefully upon receiving termination signals.
Tested the logger in various parts of the bot to ensure consistent behavior across the application.
Description:
This PR refactors the existing logging implementation to enhance modularity, reusability, and performance for cognibot. The key improvements include:
Encapsulation of Logging Logic:
Logger
class inlogger.py
that encapsulates the entire logging setup.AsyncRotatingFileHandler
for non-blocking log writes.Asynchronous Log Handling:
RotatingFileHandler
with a customAsyncRotatingFileHandler
, which uses aThreadPoolExecutor
to asynchronously handle log writing. This prevents I/O operations from blocking the main application flow, improving performance.Signal Handling Integration:
logger.py
file, ensuring that the logger gracefully shuts down when the application receives termination signals (e.g., SIGTERM, SIGINT).setup_signal_handlers
method in theLogger
class.Simplified Import and Usage:
app_logger
) is now set up inlogger.py
and can be easily imported into other files, maintaining a consistent logging setup across the application.app_logger
in any file will give access to the pre-configured logger.Changes Made:
AsyncRotatingFileHandler
class for asynchronous log handling.Logger
class.app_logger
across the codebase.Impact:
app_logger
fromlogger.py
.Testing: