diff --git a/app/main.py b/app/main.py index 8be3725..a70f0b8 100644 --- a/app/main.py +++ b/app/main.py @@ -21,6 +21,26 @@ from yt_dlp.version import __version__ as yt_dlp_version log = logging.getLogger('main') +def parseLogLevel(logLevel): + match logLevel: + case 'DEBUG': + return logging.DEBUG + case 'INFO': + return logging.INFO + case 'WARNING': + return logging.WARNING + case 'ERROR': + return logging.ERROR + case 'CRITICAL': + return logging.CRITICAL + case _: + return None + +# Configure logging before Config() uses it so early messages are not dropped. +# Only configure if no handlers are set (avoid clobbering hosting app settings). +if not logging.getLogger().hasHandlers(): + logging.basicConfig(level=parseLogLevel(os.environ.get('LOGLEVEL', 'INFO')) or logging.INFO) + class Config: _DEFAULTS = { 'DOWNLOAD_DIR': '.', @@ -112,6 +132,10 @@ class Config: return (True, '') config = Config() +# Align root logger level with Config (keeps a single source of truth). +# This re-applies the log level after Config loads, in case LOGLEVEL was +# overridden by config file settings or differs from the environment variable. +logging.getLogger().setLevel(parseLogLevel(str(config.LOGLEVEL)) or logging.INFO) class ObjectSerializer(json.JSONEncoder): def default(self, obj): @@ -386,21 +410,6 @@ def supports_reuse_port(): except (AttributeError, OSError): return False -def parseLogLevel(logLevel): - match logLevel: - case 'DEBUG': - return logging.DEBUG - case 'INFO': - return logging.INFO - case 'WARNING': - return logging.WARNING - case 'ERROR': - return logging.ERROR - case 'CRITICAL': - return logging.CRITICAL - case _: - return None - def isAccessLogEnabled(): if config.ENABLE_ACCESSLOG: return access_logger @@ -408,7 +417,7 @@ def isAccessLogEnabled(): return None if __name__ == '__main__': - logging.basicConfig(level=parseLogLevel(config.LOGLEVEL)) + logging.getLogger().setLevel(parseLogLevel(config.LOGLEVEL) or logging.INFO) log.info(f"Listening on {config.HOST}:{config.PORT}") if config.HTTPS: diff --git a/app/ytdl.py b/app/ytdl.py index 4cffda9..5e3cbb1 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -83,6 +83,7 @@ class Download: def _download(self): log.info(f"Starting download for: {self.info.title} ({self.info.url})") try: + debug_logging = logging.getLogger().isEnabledFor(logging.DEBUG) def put_status(st): self.status_queue.put({k: v for k, v in st.items() if k in ( 'tmpfilename', @@ -105,7 +106,8 @@ class Download: self.status_queue.put({'status': 'finished', 'filename': filename}) ret = yt_dlp.YoutubeDL(params={ - 'quiet': True, + 'quiet': not debug_logging, + 'verbose': debug_logging, 'no_color': True, 'paths': {"home": self.download_dir, "temp": self.temp_dir}, 'outtmpl': { "default": self.output_template, "chapter": self.output_template_chapter }, @@ -314,8 +316,10 @@ class DownloadQueue: asyncio.create_task(self.notifier.completed(download.info)) def __extract_info(self, url, playlist_strict_mode): + debug_logging = logging.getLogger().isEnabledFor(logging.DEBUG) return yt_dlp.YoutubeDL(params={ - 'quiet': True, + 'quiet': not debug_logging, + 'verbose': debug_logging, 'no_color': True, 'extract_flat': True, 'ignore_no_formats_error': True,