Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store activated loggers globally #223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/whats_new/version_ongoing.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
Version ongoing
---------------

Remove redundancy of activate_log configuration parameter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In order to activate external loggers, it was previously necessary to
specify their names for each defined auxiliary.

This is no longer the case and specifying them in only one auxiliary
will be enough for the loggers to stay enabled.
19 changes: 14 additions & 5 deletions src/pykiso/logging_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ class LogOptions(NamedTuple):
verbose: bool


# use to store the selected logging options
# used to store the selected logging options
log_options: Optional[LogOptions] = None

# used to store the loggers that shouldn't be silenced
active_loggers = set()


def get_logging_options() -> LogOptions:
"""Simply return the previous logging options.
Expand Down Expand Up @@ -164,7 +167,7 @@ def add_logging_level(level_name: str, level_num: int):

Example
-------
>>> addLoggingLevel('TRACE', logging.DEBUG - 5)
>>> add_logging_level('KISO', logging.DEBUG - 5)
>>> logging.getLogger(__name__).setLevel("KISO")
>>> logging.getLogger(__name__).trace('that worked')
>>> logging.kiso('so did this')
Expand Down Expand Up @@ -195,15 +198,17 @@ def initialize_loggers(loggers: Optional[List[str]]) -> None:

:param loggers: list of logger names to keep activated
"""
global active_loggers
if loggers is None:
loggers = list()
# keyword 'all' should keep all loggers to the configured level
if "all" in loggers:
logging.internal_warning(
"All loggers are activated, this could lead to performance issues."
)
active_loggers |= set(logging.root.manager.loggerDict.keys())
return
# keep package and auxiliary loggers
# keep package and auxiliary loggers, store all the others to deactivate them
relevant_loggers = {
name: logger
for name, logger in logging.root.manager.loggerDict.items()
Expand All @@ -218,7 +223,11 @@ def initialize_loggers(loggers: Optional[List[str]]) -> None:
if (logger.startswith(parent) or parent.startswith(logger))
]
loggers += childs
# keep original level for specified loggers
loggers_to_deactivate = set(relevant_loggers) - set(loggers)

# store previous loggers to keep active (union of previous and current loggers)
active_loggers |= set(loggers)

# set the loggers that are not part of active_loggers to level WARNING
loggers_to_deactivate = set(relevant_loggers) - set(active_loggers)
for logger_name in loggers_to_deactivate:
logging.getLogger(logger_name).setLevel(logging.WARNING)