Python 在 Fastapi 应用程序上使用 loguru-log 请求参数进行记录
Python Logging with loguru- log request params on Fastapi app
我有一个 fastapi 应用程序,我想记录对它发出的每个请求。我正在尝试为此使用 loguru 和 uvicorn,但我不知道如何打印与每个请求关联的 headers 和请求参数(如果有的话)。
我想要这样的东西:
INFO 2020-08-13 13:36:33.494 uvicorn.protocols.http.h11_impl:send - 127.0.0.1:52660 - "GET
/url1/url2/ HTTP/1.1" 400 params={"some": value, "some1":value}
有办法吗?感谢您的帮助。
这里有一些链接:
您可以使用中间件记录每个请求:
import sys
import uvicorn
from fastapi import FastAPI, Request
from loguru import logger
from starlette.routing import Match
logger.remove()
logger.add(sys.stdout, colorize=True, format="<green>{time:HH:mm:ss}</green> | {level} | <level>{message}</level>")
app = FastAPI()
@app.middleware("http")
async def log_middle(request: Request, call_next):
logger.debug(f"{request.method} {request.url}")
routes = request.app.router.routes
logger.debug("Params:")
for route in routes:
match, scope = route.matches(request)
if match == Match.FULL:
for name, value in scope["path_params"].items():
logger.debug(f"\t{name}: {value}")
logger.debug("Headers:")
for name, value in request.headers.items():
logger.debug(f"\t{name}: {value}")
response = await call_next(request)
return response
@app.get("/{param1}/{param2}")
async def path_operation(param1: str, param2: str):
return {'param1': param1, 'param2': param2}
if __name__ == "__main__":
uvicorn.run("app:app", host="localhost", port=8001)
更新:也可以使用对路由器级别的依赖(感谢下面评论中的@lsabi):
import sys
import uvicorn
from fastapi import FastAPI, Request, APIRouter, Depends
from loguru import logger
from starlette.routing import Match
logger.remove()
logger.add(sys.stdout, colorize=True, format="<green>{time:HH:mm:ss}</green> | {level} | <level>{message}</level>")
app = FastAPI()
router = APIRouter()
async def logging_dependency(request: Request):
logger.debug(f"{request.method} {request.url}")
logger.debug("Params:")
for name, value in request.path_params.items():
logger.debug(f"\t{name}: {value}")
logger.debug("Headers:")
for name, value in request.headers.items():
logger.debug(f"\t{name}: {value}")
@router.get("/{param1}/{param2}")
async def path_operation(param1: str, param2: str):
return {'param1': param1, 'param2': param2}
app.include_router(router, dependencies=[Depends(logging_dependency)])
if __name__ == "__main__":
uvicorn.run("app:app", host="localhost", port=8001)
curl http://localhost:8001/admin/home
输出:
16:06:43 | DEBUG | GET http://localhost:8001/admin/home
16:06:43 | DEBUG | Params:
16:06:43 | DEBUG | param1: admin
16:06:43 | DEBUG | param2: home
16:06:43 | DEBUG | Headers:
16:06:43 | DEBUG | host: localhost:8001
16:06:43 | DEBUG | user-agent: curl/7.64.0
16:06:43 | DEBUG | accept: */*
我有一个 fastapi 应用程序,我想记录对它发出的每个请求。我正在尝试为此使用 loguru 和 uvicorn,但我不知道如何打印与每个请求关联的 headers 和请求参数(如果有的话)。
我想要这样的东西:
INFO 2020-08-13 13:36:33.494 uvicorn.protocols.http.h11_impl:send - 127.0.0.1:52660 - "GET
/url1/url2/ HTTP/1.1" 400 params={"some": value, "some1":value}
有办法吗?感谢您的帮助。
这里有一些链接:
您可以使用中间件记录每个请求:
import sys
import uvicorn
from fastapi import FastAPI, Request
from loguru import logger
from starlette.routing import Match
logger.remove()
logger.add(sys.stdout, colorize=True, format="<green>{time:HH:mm:ss}</green> | {level} | <level>{message}</level>")
app = FastAPI()
@app.middleware("http")
async def log_middle(request: Request, call_next):
logger.debug(f"{request.method} {request.url}")
routes = request.app.router.routes
logger.debug("Params:")
for route in routes:
match, scope = route.matches(request)
if match == Match.FULL:
for name, value in scope["path_params"].items():
logger.debug(f"\t{name}: {value}")
logger.debug("Headers:")
for name, value in request.headers.items():
logger.debug(f"\t{name}: {value}")
response = await call_next(request)
return response
@app.get("/{param1}/{param2}")
async def path_operation(param1: str, param2: str):
return {'param1': param1, 'param2': param2}
if __name__ == "__main__":
uvicorn.run("app:app", host="localhost", port=8001)
更新:也可以使用对路由器级别的依赖(感谢下面评论中的@lsabi):
import sys
import uvicorn
from fastapi import FastAPI, Request, APIRouter, Depends
from loguru import logger
from starlette.routing import Match
logger.remove()
logger.add(sys.stdout, colorize=True, format="<green>{time:HH:mm:ss}</green> | {level} | <level>{message}</level>")
app = FastAPI()
router = APIRouter()
async def logging_dependency(request: Request):
logger.debug(f"{request.method} {request.url}")
logger.debug("Params:")
for name, value in request.path_params.items():
logger.debug(f"\t{name}: {value}")
logger.debug("Headers:")
for name, value in request.headers.items():
logger.debug(f"\t{name}: {value}")
@router.get("/{param1}/{param2}")
async def path_operation(param1: str, param2: str):
return {'param1': param1, 'param2': param2}
app.include_router(router, dependencies=[Depends(logging_dependency)])
if __name__ == "__main__":
uvicorn.run("app:app", host="localhost", port=8001)
curl http://localhost:8001/admin/home
输出:
16:06:43 | DEBUG | GET http://localhost:8001/admin/home
16:06:43 | DEBUG | Params:
16:06:43 | DEBUG | param1: admin
16:06:43 | DEBUG | param2: home
16:06:43 | DEBUG | Headers:
16:06:43 | DEBUG | host: localhost:8001
16:06:43 | DEBUG | user-agent: curl/7.64.0
16:06:43 | DEBUG | accept: */*