mirror of
https://github.com/infinition/Bjorn.git
synced 2025-12-06 06:11:46 +00:00
Clone
1
logger.py
infinition edited this page 2024-07-06 01:09:03 +02:00
logger.py Logger
This document describes the detailed step-by-step process of how the logger.py script works, including the specific methods, classes, and functions used at each step.
Initialization and Start of Logger
Importing Modules
- The
logger.pyscript imports several modules, including standard libraries (logging,os) and third-party libraries (RotatingFileHandlerfromlogging.handlers,Console,RichHandler,Themefromrich).
Custom Log Level
- Custom Log Level "SUCCESS":
- A custom log level named "SUCCESS" is defined with a numeric value of 25.
- A method
successis added to thelogging.Loggerclass to log messages at this custom level.
VerticalFilter Class
- Purpose: Excludes specific log messages containing the word "Vertical".
- Key Steps:
- Filter Method:
- The
filtermethod returnsFalseif the log message contains "Vertical", excluding such messages from the logs.
- The
- Filter Method:
Logger Class
__init__ Method
- Purpose: Initializes the logger with handlers for both console and file output.
- Key Steps:
- Set Up Logger:
- Creates a logger instance and sets the log level.
- Define Custom Log Level Styles:
- Defines a custom theme for different log levels using the
Themeclass fromrich.
- Defines a custom theme for different log levels using the
- Set Up Console Handler:
- Creates a console handler using
RichHandlerfor enhanced console output. - Sets the log level and formatter for the console handler.
- Creates a console handler using
- Ensure Log Directory Exists:
- Ensures the log directory exists by creating it if necessary.
- Set Up File Handler:
- Creates a file handler using
RotatingFileHandlerto manage log file sizes and backups. - Sets the log level and formatter for the file handler.
- Creates a file handler using
- Add Filters to Handlers:
- Adds the
VerticalFilterto both console and file handlers to exclude specific log messages.
- Adds the
- Add Handlers to Logger:
- Adds both the console and file handlers to the logger.
- Set Up Logger:
set_level Method
- Purpose: Dynamically adjusts the log level of the logger and its handlers.
- Key Steps:
- Set Log Level:
- Sets the log level for the logger and all its handlers.
- Set Log Level:
Logging Methods
- Purpose: Provides methods to log messages at various levels.
- Key Steps:
- Log Methods:
debug(message): Logs a debug message.info(message): Logs an info message.warning(message): Logs a warning message.error(message): Logs an error message.critical(message): Logs a critical message.success(message): Logs a success message.
- Log Methods:
disable_logging Method
- Purpose: Disables all logging.
- Key Steps:
- Disable Logging:
- Disables logging by setting the logging level to
CRITICAL, the highest severity level.
- Disables logging by setting the logging level to
- Disable Logging:
Example Usage
- An example usage is provided to demonstrate how to create a
Loggerinstance, log messages at various levels, change the log level, and disable logging.
Execution Flow when logger.py is Run
Step 1: Initialize Logger
- An instance of the
Loggerclass is created with a specified name and log level.
Step 2: Log Messages
- The logger logs messages at various levels (debug, info, warning, error, critical, success) using the corresponding methods.
Step 3: Change Log Level
- The log level is changed dynamically using the
set_levelmethod, filtering out messages below the new log level.
Step 4: Disable Logging
- Logging is disabled using the
disable_loggingmethod, preventing any further messages from being logged.
Summary
The logger.py script sets up a robust logging system for the Bjorn project. It defines custom logging levels and formats, integrates with the Rich library for enhanced console output, and ensures logs are written to rotating files for persistence. The Logger class provides methods to log messages at various levels, dynamically adjust log levels, and disable logg