101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
"""
|
|
this module contains all tests logging-related :
|
|
|
|
- the logger should have at least one StreamHandler and one FileHandler each time
|
|
logwatcher is launched.
|
|
- the StreamHandler must watch at an error level
|
|
- the FileHandler must watch at a debug
|
|
level
|
|
-
|
|
"""
|
|
import re
|
|
from logging import DEBUG, ERROR, INFO, WARNING, FileHandler, StreamHandler, getLogger
|
|
|
|
import pytest
|
|
|
|
from logwatcher import logger
|
|
from logwatcher.logging import _setup_formatter, _setup_handler, setup_logging
|
|
|
|
|
|
def test_setup_handler(tmp_log_file):
|
|
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):
|
|
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():
|
|
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 == '%d/%m/%Y %H:%M:%S'
|
|
|
|
def test_modified_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):
|
|
# 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 critical to warning
|
|
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])
|
|
|