From e0058d969f7675fb2cc6ff4260fbd6ac7627ea91 Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Thu, 27 Feb 2025 00:37:59 +0300 Subject: [PATCH] added file logging and automatically load config/cookies.txt if non is given via ytdlp.json file. --- README.md | 1 + app/library/config.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index becf113f..87a72fc0 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Certain values can be set via environment variables, using the `-e` parameter on * __YTP_BASIC_MODE__: Whether to run WebUI in basic mode. Defaults to `false`. In basic mode, A minimal UI will be shown, the majority of the features will be disabled. * __YTP_DEFAULT_PRESET__: The default preset to use for the download. Defaults to `default`. * __YTP_INSTANCE_TITLE__: The title of the instance. Defaults to empty string. +* __YTP_FILE_LOGGING__: Whether to log to file. Defaults to `false`. ## Running behind a reverse proxy diff --git a/app/library/config.py b/app/library/config.py index 914f6ff9..3b768ef5 100644 --- a/app/library/config.py +++ b/app/library/config.py @@ -5,6 +5,7 @@ import os import re import sys import time +from logging.handlers import RotatingFileHandler from multiprocessing.managers import SyncManager from pathlib import Path @@ -139,6 +140,9 @@ class Config: instance_title: str | None = None "The title of the instance." + file_logging: bool = False + "Enable file logging." + _manual_vars: tuple = ( "temp_path", "config_path", @@ -179,6 +183,7 @@ class Config: "ui_update_title", "pip_ignore_updates", "basic_mode", + "file_logging", ) "The variables that are booleans." @@ -359,6 +364,12 @@ class Config: LOG.info("keep archive option is enabled.") self.ytdl_options["download_archive"] = os.path.join(self.config_path, "archive.log") + cookiesFile: str = os.path.join(self.config_path, "cookies.txt") + + if os.path.exists(cookiesFile) and self.ytdl_options.get("cookiefile", None) is None: + LOG.info(f"Using cookies from '{cookiesFile}' as default.") + self.ytdl_options["cookiefile"] = cookiesFile + if self.temp_keep: LOG.info("Keep temp files option is enabled.") @@ -368,6 +379,15 @@ class Config: if self.basic_mode: LOG.info("The frontend is running in basic mode.") + if self.file_logging: + handler = RotatingFileHandler( + os.path.join(self.config_path, "app.log"), maxBytes=1 * 1024 * 1024, backupCount=3 + ) + handler.setLevel(logging.ERROR) + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + handler.setFormatter(formatter) + logging.getLogger().addHandler(handler) + self.started = time.time() def _get_attributes(self) -> dict: