Skip to content
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
32 changes: 32 additions & 0 deletions fancylog/fancylog.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def start_logging(
log_to_console=True,
timestamp=True,
logger_name=None,
third_party_loggers=None,
third_party_log_level="WARNING",
):
"""Prepare the log file, and then begin logging.

Expand Down Expand Up @@ -83,6 +85,12 @@ def start_logging(
logger_name
If None, logger uses default logger; otherwise, logger
name is set to `logger_name`.
third_party_loggers
Names of loggers (e.g. verbose dependencies) to pin to
`third_party_log_level`, overriding the level they would
otherwise inherit. Default: None.
third_party_log_level
Level to set `third_party_loggers` to. Default: 'WARNING'.

Returns
-------
Expand Down Expand Up @@ -131,6 +139,8 @@ def start_logging(
multiprocessing_aware=multiprocessing_aware,
log_to_console=log_to_console,
logger_name=logger_name,
third_party_loggers=third_party_loggers,
third_party_log_level=third_party_log_level,
)
return logging_file

Expand Down Expand Up @@ -475,6 +485,8 @@ def initialise_logger(
file_level="DEBUG",
log_to_console=True,
logger_name=None,
third_party_loggers=None,
third_party_log_level="WARNING",
):
"""Set up (possibly multiprocessing aware) logging.

Expand All @@ -491,6 +503,11 @@ def initialise_logger(
logger_name
If None, logger uses default logger. Otherwise, logger name
is set to `logger_name`.
third_party_loggers
Names of loggers to pin to `third_party_log_level`, overriding
the level they would otherwise inherit. Default: None.
third_party_log_level
Level to set `third_party_loggers` to. Default: 'WARNING'.

"""
if logger_name:
Expand All @@ -502,6 +519,12 @@ def initialise_logger(

logger.setLevel(getattr(logging, file_level))

if third_party_loggers:
for name in third_party_loggers:
logging.getLogger(name).setLevel(
getattr(logging, third_party_log_level)
)

formatter = logging.Formatter(
"%(asctime)s - %(levelname)s"
" - %(processName)s %(filename)s:%(lineno)s"
Expand Down Expand Up @@ -531,6 +554,8 @@ def setup_logging(
multiprocessing_aware=True,
log_to_console=True,
logger_name=None,
third_party_loggers=None,
third_party_log_level="WARNING",
):
"""Set up (possibly multiprocessing-aware) logging.

Expand All @@ -548,6 +573,11 @@ def setup_logging(
If True, logs will also be printed to the console. Default is True.
logger_name
Name of the logger to use. If None, the default logger is used.
third_party_loggers
Names of loggers to pin to `third_party_log_level`, overriding
the level they would otherwise inherit. Default: None.
third_party_log_level
Level to set `third_party_loggers` to. Default: 'WARNING'.

"""
if multiprocessing_aware and logger_name:
Expand All @@ -572,6 +602,8 @@ def setup_logging(
file_level=file_level,
log_to_console=log_to_console,
logger_name=logger_name,
third_party_loggers=third_party_loggers,
third_party_log_level=third_party_log_level,
)

if multiprocessing_aware:
Expand Down
16 changes: 16 additions & 0 deletions tests/tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ def test_logger_name(tmp_path):
assert logger_name in logging.root.manager.loggerDict


def test_third_party_log_level(tmp_path):
"""Loggers named in `third_party_loggers` are pinned to
`third_party_log_level`, independent of `file_log_level`.
"""
fancylog.start_logging(
tmp_path,
fancylog,
file_log_level="DEBUG",
third_party_loggers=["some_noisy_dependency"],
third_party_log_level="WARNING",
)

assert logging.getLogger("some_noisy_dependency").level == logging.WARNING
assert logging.getLogger().level == logging.DEBUG


def test_assert_named_logger_with_multiprocessing(tmp_path):
"""Test an error is raised if trying to use multiprocess
logging with a named logger.
Expand Down
Loading