Wie bekomme ich das Konsolenprotokoll meiner Fastapi -Anwendung im JSON -Format mit einer anderen Struktur und unterschiPython

Python-Programme
Anonymous
 Wie bekomme ich das Konsolenprotokoll meiner Fastapi -Anwendung im JSON -Format mit einer anderen Struktur und unterschi

Post by Anonymous »

Ich habe eine FastAPI -Anwendung, in der ich die Standardprotokolle mit den folgenden Daten im JSON -Format in STDOut schriftlich erhalten möchte:
App -Protokolle sollten so aussehen:

Code: Select all

{
"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)"
}
}
}
Zugriffsprotokolle sollten so aussehen:

Code: Select all

{
"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": {
"statusCode": 200,
"status": "OK"
}
}
}
}
Was ich ausprobiert habe:
Ich habe versucht, das JSON-Logging dafür zu verwenden. Mit diesem Beispiel kann ich auf die Anforderungsprotokolle in JSON zugreifen und die Struktur ändern. Ich kann jedoch nicht finden, wie ich auf die App -Protokolle zugreift und ändern soll.

Code: Select all

{"written_at": "2022-01-28T09:31:38.686Z", "written_ts": 1643362298686910000, "msg":
"Started server process [12919]", "type": "log", "logger": "uvicorn.error", "thread":
"MainThread", "level": "INFO", "module": "server", "line_no": 82, "correlation_id":
"-"}

{"written_at": "2022-01-28T09:31:38.739Z", "written_ts": 1643362298739838000, "msg":
"Started server process [12919]", "type": "log", "logger": "uvicorn.error", "thread":
"MainThread", "level": "INFO", "module": "server", "line_no": 82, "correlation_id":
"-"}

{"written_at": "2022-01-28T09:31:38.739Z", "written_ts": 1643362298739951000, "msg":
"Waiting for application startup.", "type": "log", "logger": "uvicorn.error",
"thread": "MainThread", "level": "INFO", "module": "on", "line_no": 45,
"correlation_id": "-"}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post