error log test
This commit is contained in:
parent
f756122128
commit
ae3d0966b1
3 changed files with 40 additions and 5 deletions
|
|
@ -46,6 +46,8 @@ def output_summary(subscriptions: List[Subscription]) -> None:
|
||||||
-------
|
-------
|
||||||
Output summary to print
|
Output summary to print
|
||||||
"""
|
"""
|
||||||
|
# many locals for proper output printing
|
||||||
|
# pylint: disable=too-many-locals
|
||||||
if len(subscriptions) == 0:
|
if len(subscriptions) == 0:
|
||||||
logger.info("No subscriptions ran")
|
logger.info("No subscriptions ran")
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,11 @@ class Logger:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def error_log_filename(cls) -> str:
|
def error_log_filename(cls) -> str:
|
||||||
|
"""
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
File name of the error log file
|
||||||
|
"""
|
||||||
return cls._ERROR_LOG_FILE.name
|
return cls._ERROR_LOG_FILE.name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
@ -208,7 +213,10 @@ class Logger:
|
||||||
@classmethod
|
@classmethod
|
||||||
def log_exception(cls, exception: Exception, log_filepath: Optional[Path] = None):
|
def log_exception(cls, exception: Exception, log_filepath: Optional[Path] = None):
|
||||||
"""
|
"""
|
||||||
Performs the final log before exiting from an error
|
Logs an exception based on the exception type. Will transfer all
|
||||||
|
debug logs into the error log file. This allows for subscriptions to only write to the
|
||||||
|
error log if an error occurred - successful subscriptions will clean the debug log file
|
||||||
|
w/out any write to the error log.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
|
|
||||||
|
|
@ -111,11 +111,12 @@ class TestLogger:
|
||||||
Logger.cleanup()
|
Logger.cleanup()
|
||||||
assert not os.path.isfile(Logger._DEBUG_LOGGER_FILE.name)
|
assert not os.path.isfile(Logger._DEBUG_LOGGER_FILE.name)
|
||||||
|
|
||||||
def test_logger_can_be_cleaned_during_execution(self):
|
@pytest.mark.parametrize("clean_error_log", [True, False])
|
||||||
|
def test_logger_can_be_cleaned_during_execution(self, clean_error_log: bool):
|
||||||
Logger._LOGGER_LEVEL = LoggerLevels.INFO
|
Logger._LOGGER_LEVEL = LoggerLevels.INFO
|
||||||
logger = Logger.get(name="name_test")
|
logger = Logger.get(name="name_test")
|
||||||
|
|
||||||
for _ in range(2):
|
for iteration in range(2):
|
||||||
logger.info("info test")
|
logger.info("info test")
|
||||||
logger.debug("debug test")
|
logger.debug("debug test")
|
||||||
|
|
||||||
|
|
@ -127,8 +128,32 @@ class TestLogger:
|
||||||
"[ytdl-sub:name_test] debug test\n",
|
"[ytdl-sub:name_test] debug test\n",
|
||||||
]
|
]
|
||||||
|
|
||||||
Logger.cleanup()
|
try:
|
||||||
assert not os.path.isfile(Logger._DEBUG_LOGGER_FILE.name)
|
raise ValueError("some error")
|
||||||
|
except ValueError as exc:
|
||||||
|
Logger.log_exception(exception=exc)
|
||||||
|
|
||||||
|
Logger.cleanup(cleanup_error_log=clean_error_log)
|
||||||
|
assert not os.path.isfile(Logger.debug_log_filename())
|
||||||
|
|
||||||
|
assert clean_error_log == (not os.path.isfile(Logger.error_log_filename()))
|
||||||
|
if not clean_error_log:
|
||||||
|
with open(Logger.error_log_filename(), mode="r", encoding="utf-8") as err_file:
|
||||||
|
err_logs = err_file.readlines()
|
||||||
|
expected = [
|
||||||
|
"[ytdl-sub:name_test] info test\n",
|
||||||
|
"[ytdl-sub:name_test] debug test\n",
|
||||||
|
"[ytdl-sub] An uncaught error occurred:\n",
|
||||||
|
"Traceback (most recent call last):\n",
|
||||||
|
' File "/home/j/workspace/ytdl-sub/tests/unit/utils/test_logger.py", line 132, in test_logger_can_be_cleaned_during_execution\n',
|
||||||
|
' raise ValueError("some error")\n',
|
||||||
|
"ValueError: some error\n",
|
||||||
|
"[ytdl-sub] Version 2023.03.24+14e4a4b\n",
|
||||||
|
f"Please upload the error log file '{Logger.error_log_filename()}' and make a Github issue at https://github.com/jmbannon/ytdl-sub/issues with your config and command/subscription yaml file to reproduce. Thanks for trying ytdl-sub!\n",
|
||||||
|
]
|
||||||
|
if iteration == 1:
|
||||||
|
expected.extend(expected) # Two errors occurred, error log should contain 2
|
||||||
|
assert err_logs == expected
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"log_level, expected_stdout",
|
"log_level, expected_stdout",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue