Skip to content

Commit

Permalink
Fix modified logger in add_file_handler, add tests
Browse files Browse the repository at this point in the history
It appears that the `logger` parameter was previously unused, and that
the function just used `colcon_logger` directly. As it happens, the only
use of `add_file_handler` passes `colcon_logger`, so the mistake had no
effect.

This fixes the bug and adds a simple test for that code.
  • Loading branch information
cottsay committed May 24, 2024
1 parent 15ed7d6 commit 0802431
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions colcon_core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ def filter(self, record): # noqa: A003
if isinstance(handler, logging.StreamHandler):
formatter = handler.formatter
# filter colcon specific log messages from default stream handler
handler.addFilter(Filter(colcon_logger.name))
handler.addFilter(Filter(logger.name))

# add a stream handler replacing the one filtered on the root logger
handler = logging.StreamHandler()
if formatter:
# use same formatter as for stream handler
handler.setFormatter(formatter)
handler.setLevel(colcon_logger.getEffectiveLevel())
colcon_logger.addHandler(handler)
handler.setLevel(logger.getEffectiveLevel())
logger.addHandler(handler)

# add a file handler writing all log levels
handler = logging.FileHandler(str(path))
Expand All @@ -121,9 +121,9 @@ def format_message_with_relative_time(record):
# use same formatter as for stream handler
handler.setFormatter(formatter)
handler.setLevel(1)
colcon_logger.addHandler(handler)
logger.addHandler(handler)

# change the logger to handle all levels
colcon_logger.setLevel(1)
logger.setLevel(1)

return handler
1 change: 1 addition & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ testcase
testsfailed
testsuite
thomas
tmpdir
todo
traceback
tryfirst
Expand Down
20 changes: 20 additions & 0 deletions test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# Licensed under the Apache License, Version 2.0

import logging
from pathlib import Path
from unittest.mock import Mock

from colcon_core.logging import add_file_handler
from colcon_core.logging import get_numeric_log_level
from colcon_core.logging import set_logger_level_from_env
import pytest
Expand Down Expand Up @@ -56,3 +58,21 @@ def test_get_numeric_log_level():
with pytest.raises(ValueError) as e:
get_numeric_log_level('-1')
assert str(e.value).endswith('numeric log levels must be positive')


def test_add_file_handler(tmpdir):
log_path = Path(tmpdir) / 'test_add_file_handler.log'
log_path.touch()
logger = logging.getLogger('test_add_file_handler')
try:
logger.setLevel(logging.WARN)
add_file_handler(logger, log_path)
assert logger.getEffectiveLevel() != logging.WARN
logger.info('test_add_file_handler')
finally:
for handler in logger.handlers:
logger.removeHandler(handler)
handler.close()

# check only that we logged SOMETHING to the file
assert log_path.stat().st_size > 10

0 comments on commit 0802431

Please sign in to comment.