From ca711bbdb8ba4ce0ef990d630cd8b823c5c1214a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Wed, 17 Jul 2024 05:21:28 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/cli.py | 58 ++++++++++++----------------------------- xiaomusic/httpserver.py | 27 ++++++++++++++++++- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/xiaomusic/cli.py b/xiaomusic/cli.py index fd5fad2..85c95d6 100644 --- a/xiaomusic/cli.py +++ b/xiaomusic/cli.py @@ -78,49 +78,25 @@ def main(): xiaomusic = XiaoMusic(config) HttpInit(xiaomusic) - log_config = { - "version": 1, - "formatters": { - "default": { - "()": "uvicorn.logging.DefaultFormatter", - "format": f"%(asctime)s [{__version__}] [%(levelname)s] %(filename)s:%(lineno)d: %(message)s", - "use_colors": False, - }, - }, - "handlers": { - "default": { - "class": "logging.StreamHandler", - "formatter": "default", - "stream": "ext://sys.stdout", - }, - "file": { - "class": "logging.FileHandler", - "formatter": "default", - "filename": config.log_file, - "mode": "a", - "encoding": "utf-8", - }, - }, - "loggers": { - "uvicorn": { - "handlers": ["default", "file"], - "level": "INFO", - }, - "uvicorn.error": { - "level": "INFO", - "handlers": ["default", "file"], - "propagate": False, - }, - "uvicorn.access": { - "handlers": ["default", "file"], - "level": "INFO", - "propagate": False, - }, - }, - } + from uvicorn.config import LOGGING_CONFIG + LOGGING_CONFIG["formatters"]["access"] = { + "format": f"%(asctime)s [{__version__}] [%(levelname)s] %(filename)s:%(lineno)d: %(message)s", + "datefmt": "[%X]", + } + LOGGING_CONFIG["handlers"]["access"] = { + "level": "INFO", + "class": "logging.handlers.RotatingFileHandler", + "formatter": "access", + "filename": config.log_file, + "maxBytes": 10 * 1024 * 1024, + "backupCount": 1, + } uvicorn.run( - HttpApp, host=["::", "0.0.0.0"], port=config.port, log_config=log_config + HttpApp, + host=["::", "0.0.0.0"], + port=config.port, + log_config=LOGGING_CONFIG, ) diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index 90809a4..f641fb5 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -2,6 +2,8 @@ import asyncio import json import os import secrets +import shutil +import tempfile from contextlib import asynccontextmanager from dataclasses import asdict from typing import Annotated @@ -10,6 +12,7 @@ from fastapi import Depends, FastAPI, HTTPException, Request, status from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.staticfiles import StaticFiles from pydantic import BaseModel +from starlette.background import BackgroundTask from starlette.responses import FileResponse from xiaomusic import __version__ @@ -239,7 +242,29 @@ async def downloadjson(data: UrlInfo): def downloadlog(Verifcation=Depends(verification)): file_path = xiaomusic.config.log_file if os.path.exists(file_path): - return FileResponse(path=file_path, media_type="text/plain") + # 创建一个临时文件来保存日志的快照 + temp_file = tempfile.NamedTemporaryFile(delete=False) + try: + with open(file_path, "rb") as f: + shutil.copyfileobj(f, temp_file) + temp_file.close() + + # 使用BackgroundTask在响应发送完毕后删除临时文件 + def cleanup_temp_file(tmp_file_path): + os.remove(tmp_file_path) + + background_task = BackgroundTask(cleanup_temp_file, temp_file.name) + return FileResponse( + temp_file.name, + media_type="text/plain", + filename="xiaomusic.txt", + background=background_task, + ) + except Exception as e: + os.remove(temp_file.name) + raise HTTPException( + status_code=500, detail="Error capturing log file" + ) from e else: return {"message": "File not found."}