Skip to content

Commit

Permalink
Merge pull request #199 from mraspaud/fix-logging
Browse files Browse the repository at this point in the history
Fix logging to handler config without loggers
  • Loading branch information
mraspaud authored Feb 29, 2024
2 parents 0713a86 + 3bed599 commit aaf85a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
4 changes: 3 additions & 1 deletion trollflow2/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ def setup_queued_logging(log_queue, config=None):
def remove_handlers_from_config(config):
"""Remove handlers from config."""
config.pop("handlers", None)
for logger in config["loggers"]:
for logger in config.get("loggers", []):
config["loggers"][logger].pop("handlers", None)
if config.get("root", None):
config["root"].pop("handlers", None)


def queued_logging(func):
Expand Down
44 changes: 42 additions & 2 deletions trollflow2/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

import pytest

from trollflow2.logging import (create_logged_process, logging_on,
queued_logging)
from trollflow2.logging import (DEFAULT_LOG_CONFIG, create_logged_process,
logging_on, queued_logging)


def test_queued_logging_has_a_listener():
Expand Down Expand Up @@ -129,6 +129,7 @@ def run_subprocess(loggers):
proc = create_logged_process(target=fun, args=(loggers,))
proc.start()
proc.join()
return proc


@queued_logging
Expand Down Expand Up @@ -182,3 +183,42 @@ def duplicate_lines(contents):
"""Make sure there are no duplicate lines."""
lines = contents.strip().split("\n")
return len(lines) != len(set(lines))


def test_logging_config_without_loggers(tmp_path):
"""Test that the log configs without loggers work."""
logfile = tmp_path / "mylog"
LOG_CONFIG_TO_FILE = {'version': 1,
'formatters': {'simple': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}},
'handlers': {'file': {'class': 'logging.FileHandler',
'filename': logfile,
'formatter': 'simple'}},
"root": {"level": "DEBUG", "handlers": ["file"]}
}

with logging_on(LOG_CONFIG_TO_FILE):
run_subprocess(["foo1", "foo2"])
with open(logfile) as fd:
file_contents = fd.read()

assert not duplicate_lines(file_contents)
assert "root debug" in file_contents
assert "root info" in file_contents
assert "root warning" in file_contents


def test_default_logging_config_works_with_subprocesses(capsys):
"""Test that the default log config works."""
LOG_CONFIG_TO_FILE = DEFAULT_LOG_CONFIG
captured = capsys.readouterr()
with logging_on(LOG_CONFIG_TO_FILE):
proc = run_subprocess(["foo1", "foo2"])
captured = capsys.readouterr()
assert proc.exitcode == 0
err = captured.err
assert not duplicate_lines(err)
assert "root debug" in err
assert "root info" in err
assert "root warning" in err
assert "foo1 debug" in err
assert "foo2 debug" in err
6 changes: 3 additions & 3 deletions trollflow2/tests/test_trollflow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,9 +1552,9 @@ def test_discard_old_data(self):
with mock.patch('trollflow2.plugins.get_config_value') as get_config_value:
get_config_value.return_value = None
job = {'product_list': None, 'input_mda': {'start_time': dt.datetime(2020, 3, 18)}}
self.assertIsNone(check_metadata(job))
get_config_value.return_value = {'start_time': -2e6}
self.assertIsNone(check_metadata(job))
assert check_metadata(job) is None
get_config_value.return_value = {'start_time': -20e6}
assert check_metadata(job) is None
get_config_value.return_value = {'start_time': -60}
with self.assertRaises(AbortProcessing):
check_metadata(job)
Expand Down

0 comments on commit aaf85a9

Please sign in to comment.