Last active
July 13, 2023 11:39
-
-
Save ReallyLiri/532ba1ba9aaaf8667f8fb097c0ccc646 to your computer and use it in GitHub Desktop.
ModuleFilter.py
This file contains hidden or 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
class ModuleFilter(logging.Filter): | |
""" | |
Filters any logger from outside `root_path` to levels ERROR and above | |
""" | |
def __init__(self, root_path: str, *args, **kwargs): | |
self.root_path = root_path | |
super.__init__(*args, **kwargs) | |
def filter(self, record: logging.LogRecord) -> bool: | |
if record.pathname.startswith(self.root_path): | |
return True | |
return record.levelno >= logging.ERROR | |
# Example usage: | |
_self_pathname: str = __file__ | |
_proj_root_separator = os.path.sep + "src" + os.path.sep | |
_proj_root_pathname = _self_pathname[: _self_pathname.index(_proj_root_separator) + len(_proj_root_separator)] | |
root = logging.getLogger() | |
for handler in root.handlers: | |
handler.addFilter(ModuleFilter(_proj_root_pathname)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment