Skip to content

Commit

Permalink
Merge pull request #5 from srfoster65:test_coverage
Browse files Browse the repository at this point in the history
test: Add coverage for file related logging
  • Loading branch information
srfoster65 authored Oct 22, 2023
2 parents 396b49c + 35f4f82 commit 5085a8e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/simple_logging_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
LoggingConfigException,
LoggingHandlerException,
)
from ._file import rotate_log
from ._level import set_levels
from ._simple_logging_config import SimpleLoggingConfig

Expand All @@ -28,6 +29,7 @@
"configure_logging",
"reset_logging",
"set_levels",
"rotate_log",
"add_logging_arguments",
"add_logging_level",
"add_print_logging_level",
Expand Down
4 changes: 1 addition & 3 deletions src/simple_logging_config/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ def modify_log_file_attributes(
_set_log_file_backup_count(config_data, backup_count)


def rotate_log() -> bool:
def rotate_log() -> None:
"""Rotate the log."""
for handler in logging.getLogger().handlers:
if hasattr(handler, "doRollover"):
logger.debug("Rotating log")
handler.doRollover()
return True
return False
4 changes: 3 additions & 1 deletion src/simple_logging_config/_simple_logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def reset(self) -> None:
logger.debug("Reset simple_logging_config")
logger.debug("Removing handlers: %s", self._handlers)
for handler_name in self._handlers:
logging.root.removeHandler(self._get_handler(handler_name))
handler = self._get_handler(handler_name)
handler.close()
logging.root.removeHandler(handler)
SimpleLoggingConfig.clear()

def __str__(self):
Expand Down
67 changes: 66 additions & 1 deletion tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@
99% tested by default configuration
"""

from os import makedirs, listdir
from pathlib import Path
from shutil import rmtree
import logging

from simple_logging_config import configure_logging
import pytest

from simple_logging_config import configure_logging, rotate_log

LOGS_FOLDER = "logs"

@pytest.fixture(scope="function", autouse=True)
def configure():
"""
Ensure logging is reset after each test.
"""
yield
configure_logging().reset()
# clean up any log files created during the test
if Path(LOGS_FOLDER).exists():
rmtree(LOGS_FOLDER)


class TestLogFileSettings:
Expand All @@ -21,3 +39,50 @@ def test_log_file_path_as_filename(self):
log_file_path_as_filename = "test_as_filename.log"
configure_logging(log_file_path=log_file_path_as_filename)
assert Path(log_file_path_as_filename).is_file()

def test_log_file_path_as_path(self):
"""
Test log file path can be set as a folder
"""
log_file_path_as_path = LOGS_FOLDER
makedirs(Path(log_file_path_as_path), exist_ok=True)
configure_logging(log_file_path=log_file_path_as_path)
assert Path(log_file_path_as_path).is_dir()

def test_rotating_limits_to_5_backups(self):
"""
Test rotating handler creates 5 backups
"""
log_file_path_as_path = LOGS_FOLDER
backup_count = 5 # default
makedirs(Path(log_file_path_as_path), exist_ok=True)
configure_logging(config="rotating_file", log_file_path=log_file_path_as_path)
logger = logging.getLogger(__name__)
loop = backup_count + 2
expected = backup_count + 1 # test.log + test.log.1 .. test.log.5
for i in range(0, loop):
logger.debug("loop %s", i)
rotate_log()
backup_file_count = len(listdir(log_file_path_as_path))
assert backup_file_count == expected

def test_setting_numbr_of_backup_files(self):
"""
Test setting number of backup files
"""
log_file_path_as_path = LOGS_FOLDER
backup_count = 3
makedirs(Path(log_file_path_as_path), exist_ok=True)
configure_logging(
config="rotating_file",
backup_count=backup_count,
log_file_path=log_file_path_as_path,
)
logger = logging.getLogger(__name__)
loop = backup_count + 2
expected = backup_count + 1 # test.log + test.log.1 .. test.log.3
for i in range(0, loop):
logger.debug("loop %s", i)
rotate_log()
backup_file_count = len(listdir(log_file_path_as_path))
assert backup_file_count == expected

0 comments on commit 5085a8e

Please sign in to comment.