-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (63 loc) · 1.51 KB
/
main.py
File metadata and controls
75 lines (63 loc) · 1.51 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import logging.config
import logging
import sys
from fastapi import FastAPI
from sqlmodel import Field, SQLModel
# custom module
from logging_lib import RouterLoggingMiddleware
# Logging configuration
logging_config = {
"version": 1,
"formatters": {
"json": {
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
"format": "%(asctime)s %(process)s %(levelname)s %(name)s %(module)s %(funcName)s %(lineno)s"
}
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "json",
"stream": sys.stderr,
}
},
"root": {
"level": "DEBUG",
"handlers": [
"console"
],
"propagate": True
}
}
logging.config.dictConfig(logging_config)
# Define application
def get_application() -> FastAPI:
application = FastAPI(title="FastAPI Logging", debug=True)
return application
# Initialize application
app = get_application()
app.add_middleware(
RouterLoggingMiddleware,
logger=logging.getLogger(__name__)
)
# Define SQLModel for testing
class User(SQLModel):
first_name: str
last_name: str
email: str
# Root route that returns a User model
@app.get(
"/",
response_model=User,
)
def root():
user = User(
first_name="John",
last_name="Doe",
email="jon@doe.com"
)
return user
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)