-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from MEHRSHAD-MIRSHEKARY/bugfix/log-levels-filt…
…ering Add LoggingLevelFilter to Ensure Proper Log Level Filtering
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
|
||
|
||
class LoggingLevelFilter(logging.Filter): | ||
""" | ||
Filters log records based on their logging level. | ||
This filter is used to prevent log records from being written to log files | ||
intended for lower log levels. For example, if we have separate log | ||
files for DEBUG, INFO, WARNING, and ERROR levels, this filter ensures that | ||
a log record with level ERROR is only written to the ERROR log file, and not | ||
to the DEBUG, INFO or WARNING log files. | ||
""" | ||
|
||
def __init__(self, logging_level: int): | ||
""" | ||
Initializes a LoggingLevelFilter instance. | ||
Args: | ||
logging_level: The logging level to filter on (e.g. logging.DEBUG, logging.INFO, etc.). | ||
Returns: | ||
None | ||
""" | ||
super().__init__() | ||
self.logging_level = logging_level | ||
|
||
def filter(self, record: logging.LogRecord) -> bool: | ||
""" | ||
Filters a log record based on its level. | ||
Args: | ||
record: The log record to filter. | ||
Returns: | ||
True if the log record's level matches the specified logging level, False otherwise. | ||
""" | ||
return record.levelno == self.logging_level |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters