Skip to content

Commit 4d19bbf

Browse files
authored
Merge pull request #23 from desultory/dev
improve log level inheritance, don't touch unless specified
2 parents 9fe5145 + 32f7496 commit 4d19bbf

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "zenlib"
7-
version = "3.1.7"
7+
version = "3.1.8"
88
authors = [
99
{ name="Desultory", email="[email protected]" },
1010
]

src/zenlib/logging/loggermixin.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
__author__ = "desultory"
2-
__version__ = "1.2.0"
2+
__version__ = "1.3.0"
33

44
from logging import Logger, getLogger
55

66
from .utils import add_handler_if_not_exists, handle_additional_logging, log_init
77

88

99
class LoggerMixIn:
10+
""" A mixin class that provides logging functionality to classes that inherit from it.
11+
The logger kwarg can be passed, which will be used as the parent logger.
12+
if _log_init is set to True, the logger will log the class initialization.
13+
if _log_level is set, the logger's level will be set to that value.
14+
If _log_bump is set, the logger's level will be bumped by that amount.
15+
Othweise, the log level is not set, and the logger will use the parent's level.
16+
"""
17+
1018
def init_logger(self, args, kwargs):
1119
# Get the parent logger from the root if one was not passed
1220
parent_logger = kwargs.pop("logger") if isinstance(kwargs.get("logger"), Logger) else getLogger()
1321
# Get a child logger from the parent logger, set self.logger
1422
self.logger = parent_logger.getChild(self.__class__.__name__)
15-
# Bump the log level if _log_bump is passed
16-
self.logger.setLevel(self.logger.parent.level + kwargs.pop("_log_bump", 0))
23+
24+
if log_level := kwargs.pop("_log_level", None):
25+
# Set the logger's level if _log_level is passed
26+
self.logger.setLevel(log_level)
27+
elif log_bump := kwargs.pop("_log_bump", None):
28+
self.logger.setLevel(self.logger.parent.level + log_bump)
1729

1830
# Add a colored stream handler if one does not exist
1931
add_handler_if_not_exists(self.logger)

0 commit comments

Comments
 (0)