From ae3d0966b163a057d0455623c759fc6859509ec8 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sat, 21 Oct 2023 11:14:00 -0700 Subject: [PATCH] error log test --- src/ytdl_sub/cli/output_summary.py | 2 ++ src/ytdl_sub/utils/logger.py | 10 ++++++++- tests/unit/utils/test_logger.py | 33 ++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/ytdl_sub/cli/output_summary.py b/src/ytdl_sub/cli/output_summary.py index d57510a7..61cf2317 100644 --- a/src/ytdl_sub/cli/output_summary.py +++ b/src/ytdl_sub/cli/output_summary.py @@ -46,6 +46,8 @@ def output_summary(subscriptions: List[Subscription]) -> None: ------- Output summary to print """ + # many locals for proper output printing + # pylint: disable=too-many-locals if len(subscriptions) == 0: logger.info("No subscriptions ran") return diff --git a/src/ytdl_sub/utils/logger.py b/src/ytdl_sub/utils/logger.py index 263cd123..0b531a32 100644 --- a/src/ytdl_sub/utils/logger.py +++ b/src/ytdl_sub/utils/logger.py @@ -108,6 +108,11 @@ class Logger: @classmethod def error_log_filename(cls) -> str: + """ + Returns + ------- + File name of the error log file + """ return cls._ERROR_LOG_FILE.name @classmethod @@ -208,7 +213,10 @@ class Logger: @classmethod 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 ---------- diff --git a/tests/unit/utils/test_logger.py b/tests/unit/utils/test_logger.py index 64415a60..a66411b2 100644 --- a/tests/unit/utils/test_logger.py +++ b/tests/unit/utils/test_logger.py @@ -111,11 +111,12 @@ class TestLogger: Logger.cleanup() 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.get(name="name_test") - for _ in range(2): + for iteration in range(2): logger.info("info test") logger.debug("debug test") @@ -127,8 +128,32 @@ class TestLogger: "[ytdl-sub:name_test] debug test\n", ] - Logger.cleanup() - assert not os.path.isfile(Logger._DEBUG_LOGGER_FILE.name) + try: + 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( "log_level, expected_stdout",