Skip to content

Commit 9a4c312

Browse files
committed
Adding and example in the README.md
1 parent 859e030 commit 9a4c312

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If you're using Cloud Foundry, it might worth to check out the library [SAP/cf-p
2323
2. Lightweight, no dependencies, minimal configuration needed (1 LoC to get it working)
2424
3. Seamlessly integrate with Python native **logging** module. Support both Python 2.7.x and 3.x
2525
4. Auto extract **correlation-id** for distributed tracing [\[1\]](#1-what-is-correlation-idrequest-id)
26-
5. Support HTTP request instrumentation. Built in support for [Flask](https://github.com/pallets/flask/), [Sanic](https://github.com/channelcat/sanic), [Quart](https://gitlab.com/pgjones/quart), [Connexion](https://github.com/zalando/connexion). Extensible to support other web frameworks. PR welcome :smiley: .
26+
5. Support HTTP request instrumentation. Built in support for [FastAPI](https://fastapi.tiangolo.com/), [Flask](https://github.com/pallets/flask/), [Sanic](https://github.com/channelcat/sanic), [Quart](https://gitlab.com/pgjones/quart), [Connexion](https://github.com/zalando/connexion). Extensible to support other web frameworks. PR welcome :smiley: .
2727
6. Highly customizable: support inject arbitrary extra properties to JSON log message, override logging formatter, etc.
2828
7. Production ready, has been used in production since 2017
2929

@@ -52,8 +52,31 @@ logger.info("test logging statement")
5252
```
5353

5454
## 2.2 Web application log
55-
### Flask
55+
### FastAPI
56+
```python
57+
import datetime, logging, sys, json_logging, fastapi, uvicorn
58+
59+
app = fastapi.FastAPI()
60+
json_logging.init_fastapi(enable_json=True)
61+
json_logging.init_request_instrument(app)
62+
63+
# init the logger as usual
64+
logger = logging.getLogger("test-logger")
65+
logger.setLevel(logging.DEBUG)
66+
logger.addHandler(logging.StreamHandler(sys.stdout))
5667

68+
@app.get('/')
69+
def home():
70+
logger.info("test log statement")
71+
logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
72+
correlation_id = json_logging.get_correlation_id()
73+
return "Hello world : " + str(datetime.datetime.now())
74+
75+
if __name__ == "__main__":
76+
uvicorn.run(app, host='0.0.0.0', port=5000)
77+
```
78+
79+
### Flask
5780
```python
5881
import datetime, logging, sys, json_logging, flask
5982

0 commit comments

Comments
 (0)