相关疑难解决方法(0)

如何获取具有不同结构和不同字段的 JSON 格式的 FastAPI 应用程序控制台日志?

我有一个 FastAPI 应用程序,我希望将默认日志写入 STDOUT,并使用 JSON 格式的以下数据:

应用程序日志应如下所示:

{
 "XYZ": {
   "log": {
     "level": "info",
     "type": "app",
     "timestamp": "2022-01-16T08:30:08.181Z",
     "file": "api/predictor/predict.py",
     "line": 34,
     "threadId": 435454,
     "message": "API Server started on port 8080 (development)"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

访问日志应如下所示:

{
 "XYZ": {
   "log": {
     "level": "info",
     "type": "access",
     "timestamp": "2022-01-16T08:30:08.181Z",
     "message": "GET /app/health 200 6ms"
   },
   "req": {
     "url": "/app/health",
     "headers": {
       "host": "localhost:8080",
       "user-agent": "curl/7.68.0",
       "accept": "*/*"
     },
     "method": "GET",
     "httpVersion": "1.1",
     "originalUrl": "/app/health",
     "query": {}
   },
   "res": {
     "statusCode": 200,
     "body": …
Run Code Online (Sandbox Code Playgroud)

python logging gunicorn fastapi uvicorn

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

fastapi ×1

gunicorn ×1

logging ×1

python ×1

uvicorn ×1