Skip to content

Commit 3aaea86

Browse files
authored
Merge pull request #33 from kuldeeprishi/develop
RequestID in response Headers
2 parents 08c824b + 46d1d71 commit 3aaea86

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

README.md

Lines changed: 18 additions & 5 deletions
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

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

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

120+
### Example 4: If you want to return request id in response
121+
122+
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.
123+
124+
```python
125+
from flask_log_request_id import current_request_id
126+
127+
@app.after_request
128+
def append_request_id(response):
129+
response.headers.add('X-REQUEST-ID', current_request_id())
130+
return response
131+
```
132+
120133
## Configuration
121134

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

0 commit comments

Comments
 (0)