diff --git a/src/ytdl_sub/cli/main.py b/src/ytdl_sub/cli/main.py index f88f74e8..b6b93548 100644 --- a/src/ytdl_sub/cli/main.py +++ b/src/ytdl_sub/cli/main.py @@ -40,7 +40,7 @@ def _download_subscriptions_from_yaml_files( subscriptions: List[Subscription] = [] output: List[Tuple[Subscription, FileHandlerTransactionLog]] = [] - # Load all of the subscriptions first to perform all validation before downloading + # Load all the subscriptions first to perform all validation before downloading for path in subscription_paths: subscriptions += Subscription.from_file_path(config=config, subscription_path=path) @@ -55,6 +55,7 @@ def _download_subscriptions_from_yaml_files( output.append((subscription, transaction_log)) gc.collect() # Garbage collect after each subscription download + Logger.cleanup() # Cleanup logger after each successful subscription download return output diff --git a/src/ytdl_sub/utils/logger.py b/src/ytdl_sub/utils/logger.py index 095c86aa..3112f918 100644 --- a/src/ytdl_sub/utils/logger.py +++ b/src/ytdl_sub/utils/logger.py @@ -197,7 +197,7 @@ class Logger: @classmethod def cleanup(cls, delete_debug_file: bool = True): """ - Cleans up any log files left behind. + Cleans up any log files left behind Parameters ---------- diff --git a/tests/unit/utils/test_logger.py b/tests/unit/utils/test_logger.py index d46534bd..8d6ebec7 100644 --- a/tests/unit/utils/test_logger.py +++ b/tests/unit/utils/test_logger.py @@ -94,13 +94,28 @@ class TestLogger: assert lines == ["[ytdl-sub:name_test] info test\n", "[ytdl-sub:name_test] debug test\n"] - # Ensure the file cleans up too - for handler in logger.handlers: - handler.close() - Logger.cleanup(delete_debug_file=True) assert not os.path.isfile(Logger._DEBUG_LOGGER_FILE.name) + def test_logger_can_be_cleaned_during_execution(self): + Logger._LOGGER_LEVEL = LoggerLevels.INFO + logger = Logger.get(name="name_test") + + for _ in range(2): + logger.info("info test") + logger.debug("debug test") + + with open(Logger._DEBUG_LOGGER_FILE.name, "r", encoding="utf-8") as log_file: + lines = log_file.readlines() + + assert lines == [ + "[ytdl-sub:name_test] info test\n", + "[ytdl-sub:name_test] debug test\n", + ] + + Logger.cleanup(delete_debug_file=True) + assert not os.path.isfile(Logger._DEBUG_LOGGER_FILE.name) + @pytest.mark.parametrize( "log_level, expected_stdout", [