Skip to content

Commit

Permalink
[BACKEND] Reset debug log on every subscription (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbannon authored Mar 3, 2023
1 parent 628c0f1 commit 896a9e2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/ytdl_sub/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ytdl_sub/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def handle_external_logs(cls, name: Optional[str] = None) -> None:
@classmethod
def cleanup(cls, delete_debug_file: bool = True):
"""
Cleans up any log files left behind.
Cleans up any log files left behind
Parameters
----------
Expand Down
23 changes: 19 additions & 4 deletions tests/unit/utils/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,28 @@ def test_logger_always_outputs_to_debug_file(self, log_level):

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",
[
Expand Down

0 comments on commit 896a9e2

Please sign in to comment.