Files
logwatcher/tests/test_logging.py
T

113 lines
4.1 KiB
Python

import logging
import re
from logging import DEBUG, ERROR, INFO, WARNING, FileHandler, StreamHandler, getLogger
import pytest
from logwatcher.config import DATETIME_FORMAT
from logwatcher.logging_config import _setup_formatter, _setup_handler, setup_logging
def test_setup_handler(tmp_log_file):
"""Assert `setup_handler` instanciate one StreamHandler and one FileHandler."""
formatter = _setup_formatter()
handler = _setup_handler(formatter=formatter, level=DEBUG)
assert isinstance(handler, StreamHandler)
handler = _setup_handler(formatter=formatter, level=INFO, filename=tmp_log_file)
assert isinstance(handler, FileHandler)
def test_count_of_handler(tmp_log_file):
"""Assert `setup_handler` instanciate exactly one StreamHandler and one FileHandler.
Must have exactly two handlers in total.
"""
logger = getLogger()
count_file_handler = 0
count_stream_handler = 0
for handler in logger.handlers:
if isinstance(handler, FileHandler):
count_file_handler += 1
elif isinstance(handler, StreamHandler):
count_stream_handler += 1
else:
pass
assert count_file_handler >= 1
assert count_stream_handler >= 1
def test_type_of_handlers():
"""Assert each handlers capture the right log level and use the right log format.
FileHandler must capute DEBUG logs while StreamHandler only capture ERROR logs.
"""
logger = logging.getLogger("logwatcher")
for handler in logger.handlers:
if isinstance(handler, FileHandler):
assert handler.level == DEBUG
elif isinstance(handler, StreamHandler):
assert handler.level == ERROR
else:
pytest.fail("handlers must be either a file or a stream handler")
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
assert handler.formatter.datefmt == DATETIME_FORMAT
def test_modified_format():
"""Assert `setup_logging` allow to use another log format.
When starting, the logger is first intialized, thus two handlers are at position
1 and 2 in logger.handlers list.
"""
logger = setup_logging()
for handler in logger.handlers[2:]:
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
formatter_different_format = _setup_formatter(
format="%(asctime)s - %(levelname)s: %(message)s"
)
logger = setup_logging(formatter=formatter_different_format)
for handler in logger.handlers[4:]:
assert handler.formatter._fmt == "%(asctime)s - %(levelname)s: %(message)s"
formatter_different_datefmt = _setup_formatter(datefmt="%a %b %Y at %Hh%M")
logger = setup_logging(formatter=formatter_different_datefmt)
for handler in logger.handlers[6:]:
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
assert handler.formatter.datefmt == "%a %b %Y at %Hh%M"
def test_logging_in_correct_path(tmp_log_file):
"""Assert log file is correctly written in output directory.
Check content of the file. It must contain logs from CRITICAL to DEBUG level.
"""
logger = logging.getLogger("logwatcher")
# must be both in file and stdout
logger.critical("hi- BYE")
logger.error("hi... ok, bye")
logger.warning("hi, are you alright?")
# only in file
logger.info("hi there")
logger.debug("hi, how are you? it's been a while!")
with open(tmp_log_file) as log_file:
content = log_file.readlines()
regex = r"\[\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\] - "
assert len(content) == 5
for line in content:
assert re.search(regex, line)
# test from critical to debug logs
assert re.search(r"CRITICAL: hi- BYE", content[0])
assert re.search(r"ERROR: hi... ok, bye", content[1])
assert re.search(r"WARNING: hi, are you alright?", content[2])
assert re.search(r"INFO: hi there", content[3])
assert logger.getEffectiveLevel() < WARNING
assert re.search(r"DEBUG: hi, how are you\? it's been a while!", content[4])