-
-
Notifications
You must be signed in to change notification settings - Fork 126
logger.py
infinition edited this page Jul 5, 2024
·
1 revision
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.
- The
logger.py
script imports several modules, including standard libraries (logging
,os
) and third-party libraries (RotatingFileHandler
fromlogging.handlers
,Console
,RichHandler
,Theme
fromrich
).
-
Custom Log Level "SUCCESS":
- A custom log level named "SUCCESS" is defined with a numeric value of 25.
- A method
success
is added to thelogging.Logger
class to log messages at this custom level.
- Purpose: Excludes specific log messages containing the word "Vertical".
-
Key Steps:
-
Filter Method:
- The
filter
method returnsFalse
if the log message contains "Vertical", excluding such messages from the logs.
- The
-
Filter 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
Theme
class fromrich
.
- Defines a custom theme for different log levels using the
-
Set Up Console Handler:
- Creates a console handler using
RichHandler
for 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
RotatingFileHandler
to 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
VerticalFilter
to 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:
- 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:
- 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:
- 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:
- An example usage is provided to demonstrate how to create a
Logger
instance, log messages at various levels, change the log level, and disable logging.
- An instance of the
Logger
class is created with a specified name and log level.
- The logger logs messages at various levels (debug, info, warning, error, critical, success) using the corresponding methods.
- The log level is changed dynamically using the
set_level
method, filtering out messages below the new log level.
- Logging is disabled using the
disable_logging
method, preventing any further messages from being logged.
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