diff --git a/pyproject.toml b/pyproject.toml index 26a9c8d..cdd3622 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "zenlib" -version = "3.1.7" +version = "3.1.8" authors = [ { name="Desultory", email="dev@pyl.onl" }, ] diff --git a/src/zenlib/logging/loggermixin.py b/src/zenlib/logging/loggermixin.py index c3eca38..5f72648 100644 --- a/src/zenlib/logging/loggermixin.py +++ b/src/zenlib/logging/loggermixin.py @@ -1,5 +1,5 @@ __author__ = "desultory" -__version__ = "1.2.0" +__version__ = "1.3.0" from logging import Logger, getLogger @@ -7,13 +7,25 @@ class LoggerMixIn: + """ A mixin class that provides logging functionality to classes that inherit from it. + The logger kwarg can be passed, which will be used as the parent logger. + if _log_init is set to True, the logger will log the class initialization. + if _log_level is set, the logger's level will be set to that value. + If _log_bump is set, the logger's level will be bumped by that amount. + Othweise, the log level is not set, and the logger will use the parent's level. + """ + def init_logger(self, args, kwargs): # Get the parent logger from the root if one was not passed parent_logger = kwargs.pop("logger") if isinstance(kwargs.get("logger"), Logger) else getLogger() # Get a child logger from the parent logger, set self.logger self.logger = parent_logger.getChild(self.__class__.__name__) - # Bump the log level if _log_bump is passed - self.logger.setLevel(self.logger.parent.level + kwargs.pop("_log_bump", 0)) + + if log_level := kwargs.pop("_log_level", None): + # Set the logger's level if _log_level is passed + self.logger.setLevel(log_level) + elif log_bump := kwargs.pop("_log_bump", None): + self.logger.setLevel(self.logger.parent.level + log_bump) # Add a colored stream handler if one does not exist add_handler_if_not_exists(self.logger)