-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathfastapi_sample_app.py
56 lines (45 loc) · 1.57 KB
/
fastapi_sample_app.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
import logging
import fastapi
import json_logging
app = fastapi.FastAPI()
# init the logger as usual
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
@app.get('/')
async def home():
logger.info("test log statement")
logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
correlation_id = json_logging.get_correlation_id()
return "hello world" \
"\ncorrelation_id : " + correlation_id
@app.get('/exception')
def exception():
try:
raise RuntimeError
except BaseException as e:
logger.error("Error occurred", exc_info=e)
logger.exception("Error occurred", exc_info=e)
return "Error occurred, check log for detail"
@app.get('/exclude_from_request_instrumentation')
def exclude_from_request_instrumentation():
return "this request wont log request instrumentation information"
if __name__ == "__main__":
import uvicorn
logging_config = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'default_handler': {
'class': 'logging.StreamHandler',
'level': 'DEBUG',
},
},
'loggers': {
'': {
'handlers': ['default_handler'],
}
}
}
json_logging.init_fastapi(enable_json=True)
json_logging.init_request_instrument(app, exclude_url_patterns=[r'^/exclude_from_request_instrumentation'])
uvicorn.run(app, host='0.0.0.0', port=5000, log_level="debug", log_config=logging_config)