fix: 配置 nqi 日志并关闭打包版访问日志
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""本机接口。只监听 127.0.0.1。功能路由按目录挂上。"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from contextlib import asynccontextmanager, suppress
|
||||
|
||||
from fastapi import FastAPI
|
||||
@@ -20,8 +22,21 @@ ORIGINS = [
|
||||
]
|
||||
|
||||
|
||||
def configure_logging() -> None:
|
||||
"""让 nqi.* 的 INFO 写到标准输出。uvicorn 默认不接管这个记录器。"""
|
||||
nqi = logging.getLogger("nqi")
|
||||
nqi.setLevel(logging.INFO)
|
||||
if not any(getattr(handler, "_nqi_stdout", False) for handler in nqi.handlers):
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s"))
|
||||
handler._nqi_stdout = True
|
||||
nqi.addHandler(handler)
|
||||
nqi.propagate = False
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
configure_logging()
|
||||
keepalive = asyncio.create_task(session_keepalive())
|
||||
try:
|
||||
yield
|
||||
@@ -58,6 +73,7 @@ if __name__ == "__main__":
|
||||
|
||||
import uvicorn
|
||||
|
||||
configure_logging()
|
||||
chosen = os.environ.get("NQI_PORT")
|
||||
if chosen:
|
||||
port = int(chosen)
|
||||
|
||||
@@ -4,12 +4,13 @@ import os
|
||||
|
||||
import uvicorn
|
||||
|
||||
from app.main import app
|
||||
from app.main import app, configure_logging
|
||||
|
||||
|
||||
def main() -> None:
|
||||
configure_logging()
|
||||
port = int(os.environ["NQI_PORT"])
|
||||
uvicorn.run(app, host="127.0.0.1", port=port, log_level="info")
|
||||
uvicorn.run(app, host="127.0.0.1", port=port, log_level="info", access_log=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user