-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
60 lines (51 loc) · 1.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
import uuid
import time
from typing import Any
import rapidjson
import structlog
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse
from starlette.requests import Request
from starlette.responses import Response
app = FastAPI()
# Clear Gunicorn access log to remove duplicate requests logging
logging.getLogger("gunicorn.access").handlers.clear()
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.threadlocal.merge_threadlocal,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(serializer=rapidjson.dumps, sort_keys=True),
],
wrapper_class=structlog.BoundLogger,
context_class=dict,
cache_logger_on_first_use=True,
)
log = structlog.get_logger()
@app.middleware("http")
async def logging_middleware(request: Request, call_next) -> Response:
# clear the threadlocal context
structlog.threadlocal.clear_threadlocal()
# bind threadlocal
structlog.threadlocal.bind_threadlocal(
logger="uvicorn.access",
request_id=str(uuid.uuid4()),
cookies=request.cookies,
scope=request.scope,
url=str(request.url),
)
try:
start_time = time.time()
response: Response = await call_next(request)
finally:
process_time = time.time() - start_time
log.info(
"processed a request",
status_code=response.status_code,
process_time=process_time,
)
return response
@app.get("/ping", response_class=PlainTextResponse)
def ping() -> Any:
return "pong"