mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-14 02:20:53 +08:00
Fix logger
This commit is contained in:
37
src/main.py
37
src/main.py
@@ -1,12 +1,17 @@
|
||||
import os
|
||||
import signal
|
||||
import logging
|
||||
import multiprocessing
|
||||
import uvicorn
|
||||
import multiprocessing
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from module import app
|
||||
from module.api import router
|
||||
from module.conf import VERSION, setup_logger, settings
|
||||
from module.conf import VERSION, settings
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -14,7 +19,7 @@ logger = logging.getLogger(__name__)
|
||||
main_process = multiprocessing.Process(target=app.run)
|
||||
|
||||
|
||||
@router.get("/api/v1/restart")
|
||||
@router.get("/api/v1/restart", tags=["program"])
|
||||
async def restart():
|
||||
global main_process
|
||||
logger.info("Restarting...")
|
||||
@@ -25,18 +30,24 @@ async def restart():
|
||||
return {"status": "success"}
|
||||
|
||||
|
||||
def show_info():
|
||||
with open("icon", "r") as f:
|
||||
for line in f.readlines():
|
||||
logger.info(line.strip("\n"))
|
||||
logger.info(f"Version {VERSION} Author: EstrellaXD Twitter: https://twitter.com/Estrella_Pan")
|
||||
logger.info("GitHub: https://github.com/EstrellaXD/Auto_Bangumi/")
|
||||
logger.info("Starting AutoBangumi...")
|
||||
if VERSION != "DEV_VERSION":
|
||||
router.mount("/assets", StaticFiles(directory="templates/assets"), name="assets")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
# HTML Response
|
||||
@router.get("/{full_path:path}", response_class=HTMLResponse, tags=["html"])
|
||||
def index(request: Request):
|
||||
context = {"request": request}
|
||||
return templates.TemplateResponse("index.html", context)
|
||||
else:
|
||||
@router.get("/", status_code=302, tags=["html"])
|
||||
def index():
|
||||
return RedirectResponse("/docs")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
setup_logger()
|
||||
show_info()
|
||||
log_config = uvicorn.config.LOGGING_CONFIG
|
||||
log_config["formatters"]["default"]["fmt"] = "[%(asctime)s] %(levelname)-8s %(message)s"
|
||||
main_process.start()
|
||||
uvicorn.run(router, host="0.0.0.0", port=settings.program.webui_port)
|
||||
uvicorn.run(router, host="0.0.0.0", port=settings.program.webui_port, log_config=log_config)
|
||||
|
||||
|
||||
@@ -1,30 +1,29 @@
|
||||
import uvicorn
|
||||
from uvicorn.config import LOGGING_CONFIG
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
import logging
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from module.core import APIProcess
|
||||
from module.conf import settings, DATA_PATH, LOG_PATH, VERSION
|
||||
from module.conf import DATA_PATH, LOG_PATH, settings
|
||||
from module.utils import json_config
|
||||
from module.models.api import *
|
||||
from module.models import Config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = FastAPI()
|
||||
api_func = APIProcess()
|
||||
|
||||
if VERSION != "DEV_VERSION":
|
||||
router.mount("/assets", StaticFiles(directory="templates/assets"), name="assets")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
@router.on_event("startup")
|
||||
async def startup_event():
|
||||
logger = logging.getLogger("uvicorn.access")
|
||||
handler = logging.StreamHandler()
|
||||
handler.setLevel(logging.INFO)
|
||||
handler.setFormatter(logging.Formatter("[%(asctime)s] %(levelname)-8s %(message)s"))
|
||||
logger.addHandler(handler)
|
||||
|
||||
|
||||
@router.get("/api/v1/data")
|
||||
def get_data():
|
||||
async def get_data():
|
||||
data = json_config.load(DATA_PATH)
|
||||
return data
|
||||
|
||||
@@ -63,21 +62,3 @@ async def add_rule(info: AddRule):
|
||||
async def update_config(config: Config):
|
||||
return api_func.update_config(config)
|
||||
|
||||
if VERSION != "DEV_VERSION":
|
||||
# HTML Response
|
||||
@router.get("/{full_path:path}", response_class=HTMLResponse)
|
||||
def index(request: Request):
|
||||
context = {"request": request}
|
||||
return templates.TemplateResponse("index.html", context)
|
||||
else:
|
||||
@router.get("/", status_code=302)
|
||||
def index():
|
||||
return RedirectResponse("/docs")
|
||||
|
||||
|
||||
def run():
|
||||
LOGGING_CONFIG["formatters"]["default"]["fmt"] = "[%(asctime)s] %(levelprefix)s %(message)s"
|
||||
uvicorn.run(router, host="0.0.0.0", port=settings.program.webui_port)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import os
|
||||
import time
|
||||
import logging
|
||||
|
||||
from module.conf import settings, setup_logger, LOG_PATH, DATA_PATH, RSS_LINK
|
||||
from module.conf import settings, setup_logger, LOG_PATH, DATA_PATH, RSS_LINK, VERSION
|
||||
from module.utils import json_config
|
||||
|
||||
from module.core import DownloadClient
|
||||
@@ -61,11 +61,21 @@ def main_process(bangumi_data, download_client: DownloadClient):
|
||||
time.sleep(settings.program.sleep_time / settings.program.rename_times)
|
||||
|
||||
|
||||
def show_info():
|
||||
with open("icon", "r") as f:
|
||||
for line in f.readlines():
|
||||
logger.info(line.strip("\n"))
|
||||
logger.info(f"Version {VERSION} Author: EstrellaXD Twitter: https://twitter.com/Estrella_Pan")
|
||||
logger.info("GitHub: https://github.com/EstrellaXD/Auto_Bangumi/")
|
||||
logger.info("Starting AutoBangumi...")
|
||||
|
||||
|
||||
def run():
|
||||
setup_logger()
|
||||
# 初始化
|
||||
reset_log()
|
||||
settings.reload()
|
||||
reset_log()
|
||||
setup_logger()
|
||||
show_info()
|
||||
download_client = DownloadClient()
|
||||
download_client.init_downloader()
|
||||
if settings.rss_parser.token is None:
|
||||
|
||||
@@ -21,8 +21,9 @@ class Setting(Config):
|
||||
|
||||
|
||||
def save_config_to_file(config: Config, path: str):
|
||||
config_dict = config.dict()
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(config, f, indent=4)
|
||||
json.dump(config_dict, f, indent=4)
|
||||
logger.info(f"Config saved")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user