Skip to content

Commit 4743922

Browse files
committed
updated documentation
1 parent a69c93e commit 4743922

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The easiest way to install it is using ``pip`` from PyPI
1717
```bash
1818
pip install flask-log-request-id
1919
```
20-
20+
2121
## Usage
2222

2323
Flask-Log-Request-Id provides the `current_request_id()` function which can be used at any time to get the request
@@ -42,7 +42,7 @@ def hello():
4242
### Example 2: Parse request id and send it to to logging
4343

4444
In the following example, we will use the `RequestIDLogFilter` to inject the request id on all log events, and
45-
a custom formatter to print this information. If all these sounds unfamiliar please take a look at [python's logging
45+
a custom formatter to print this information. If all these sounds unfamiliar please take a look at [python's logging
4646
system](https://docs.python.org/3/library/logging.html)
4747

4848

@@ -101,20 +101,33 @@ app = Flask()
101101
@celery.task()
102102
def generic_add(a, b):
103103
"""Simple function to add two numbers that is not aware of the request id"""
104-
104+
105105
logging.debug('Called generic_add({}, {}) from request_id: {}'.format(a, b, current_request_id()))
106106
return a + b
107-
107+
108108
@app.route('/')
109109
def index():
110110
a, b = randint(1, 15), randint(1, 15)
111111
logging.info('Adding two random numbers {} {}'.format(a, b))
112112
return str(generic_add.delay(a, b)) # Calling the task here, will forward the request id to the workers
113113
```
114114

115-
You can follow the same logging strategy for both web application and workers using the `RequestIDLogFilter` as shown in
115+
You can follow the same logging strategy for both web application and workers using the `RequestIDLogFilter` as shown in
116116
example 1 and 2.
117117

118+
### Example 4: If you want to return request id in response
119+
120+
This will be useful while integrating with frontend where in you can get the request id from the response (be it 400 or 500) and then trace the request in logs.
121+
122+
```python
123+
from flask_log_request_id import current_request_id
124+
125+
@app.after_request
126+
def append_request_id(response):
127+
response.headers.add('X-REQUEST-ID', current_request_id())
128+
return response
129+
```
130+
118131
## Configuration
119132

120133
The following parameters can be configured through Flask's configuration system:

0 commit comments

Comments
 (0)