From 03be4c632968621c613b4e85b9c8a0ed26daca46 Mon Sep 17 00:00:00 2001 From: nixielectra <30100102+nixielectra@users.noreply.github.com> Date: Sat, 28 Jun 2025 16:40:38 +0800 Subject: [PATCH] add LOGLEVEL and ENABLE_ACCESSLOG env variables --- README.md | 2 ++ app/main.py | 32 ++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fae36ce..a172942 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ Certain values can be set via environment variables, using the `-e` parameter on * `limited`: Downloads are started concurrently but are capped by a concurrency limit. In this mode, a semaphore is used so that at most a fixed number of downloads run at any given time. * **MAX\_CONCURRENT\_DOWNLOADS** This flag is used only when **DOWNLOAD\_MODE** is set to **limited**. It specifies the maximum number of simultaneous downloads allowed. For example, if set to `5`, then at most five downloads will run concurrently, and any additional downloads will wait until one of the active downloads completes. Defaults to `3`. +* __LOGLEVEL__: Log level, can be set to `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` or `NONE`. Defaults to `DEBUG`. +* __ENABLE_ACCESSLOG__: whether to enable access log. Defaults to `true`. The project's Wiki contains examples of useful configurations contributed by users of MeTube: * [YTDL_OPTIONS Cookbook](https://github.com/alexta69/metube/wiki/YTDL_OPTIONS-Cookbook) diff --git a/app/main.py b/app/main.py index 0e9959c..0564510 100644 --- a/app/main.py +++ b/app/main.py @@ -4,6 +4,7 @@ import os import sys from aiohttp import web +from aiohttp.log import access_logger import ssl import socket import socketio @@ -48,9 +49,11 @@ class Config: 'DEFAULT_THEME': 'auto', 'DOWNLOAD_MODE': 'limited', 'MAX_CONCURRENT_DOWNLOADS': 3, + 'LOGLEVEL': 'DEBUG', + 'ENABLE_ACCESSLOG': 'true', } - _BOOLEAN = ('DOWNLOAD_DIRS_INDEXABLE', 'CUSTOM_DIRS', 'CREATE_CUSTOM_DIRS', 'DELETE_FILE_ON_TRASHCAN', 'DEFAULT_OPTION_PLAYLIST_STRICT_MODE', 'HTTPS') + _BOOLEAN = ('DOWNLOAD_DIRS_INDEXABLE', 'CUSTOM_DIRS', 'CREATE_CUSTOM_DIRS', 'DELETE_FILE_ON_TRASHCAN', 'DEFAULT_OPTION_PLAYLIST_STRICT_MODE', 'HTTPS', 'ENABLE_ACCESSLOG') def __init__(self): for k, v in self._DEFAULTS.items(): @@ -305,13 +308,34 @@ 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 + else: + return None + if __name__ == '__main__': - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(level=parseLogLevel(config.LOGLEVEL)) log.info(f"Listening on {config.HOST}:{config.PORT}") if config.HTTPS: ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain(certfile=config.CERTFILE, keyfile=config.KEYFILE) - web.run_app(app, host=config.HOST, port=int(config.PORT), reuse_port=supports_reuse_port(), ssl_context=ssl_context) + web.run_app(app, host=config.HOST, port=int(config.PORT), reuse_port=supports_reuse_port(), ssl_context=ssl_context, access_log=isAccessLogEnabled()) else: - web.run_app(app, host=config.HOST, port=int(config.PORT), reuse_port=supports_reuse_port()) + web.run_app(app, host=config.HOST, port=int(config.PORT), reuse_port=supports_reuse_port(), access_log=isAccessLogEnabled())