Skip to content

Commit

Permalink
Refresh all filehandlers on named logger and add tests. (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZiminski authored May 1, 2024
1 parent 712ca06 commit be62d33
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions fancylog/fancylog.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def initialise_logger(
"""
if logger_name:
logger = logging.getLogger(logger_name)
logger.handlers = []
else:
logger = logging.getLogger()

Expand Down
103 changes: 103 additions & 0 deletions tests/tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

from rich.logging import RichHandler

import fancylog

lateral_separator = "**************"
Expand Down Expand Up @@ -38,3 +40,104 @@ def test_logger_name(tmp_path):
fancylog.start_logging(tmp_path, fancylog, logger_name=logger_name)

assert logger_name in logging.root.manager.loggerDict


def test_correct_handlers_are_set(tmp_path):
"""
Test the handlers on the logger are as specified by the
`start_logging` call. Note this cannot be tested
on the root logger has it holds handlers that
were assigned in earlier tests.
"""
logger_name = "hello_world"

# Test no handlers are assigned when non requested
fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=False,
log_to_file=False,
)

logger = logging.getLogger(logger_name)

assert logger.handlers == []

# Test RichHandler only is assigned when requested
fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=True,
log_to_file=False,
)

assert len(logger.handlers) == 1
assert isinstance(logger.handlers[0], RichHandler)

# Test FileHandler only is assigned when requested
fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=False,
log_to_file=True,
)

assert len(logger.handlers) == 1
assert isinstance(logger.handlers[0], logging.FileHandler)

# Test both handlers are assigned when requested
fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=True,
log_to_file=True,
)

assert len(logger.handlers) == 2
assert isinstance(logger.handlers[0], logging.FileHandler)
assert isinstance(logger.handlers[1], RichHandler)


def test_handlers_are_refreshed(tmp_path):
"""
When a named logger is requested, the handlers
are reset to that handlers assigned on previous
calls are not carried over to the most recent call.
"""
logger_name = "hello_world"
fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=False,
log_to_file=False,
)

logger = logging.getLogger(logger_name)

assert logger.handlers == []

fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=True,
log_to_file=False,
)

assert len(logger.handlers) == 1
assert isinstance(logger.handlers[0], RichHandler)

fancylog.start_logging(
tmp_path,
fancylog,
logger_name=logger_name,
log_to_console=False,
log_to_file=False,
)

assert logger.handlers == []

0 comments on commit be62d33

Please sign in to comment.