You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Case 2. JSON Output Configured within Python Code](#case-2-json-output-configured-within-python-code)
@@ -64,6 +66,7 @@ All features can be fully configured from the configuration file.
64
66
-[Case 6. Add parts of Django Request to JSON Output](#case-6-add-parts-of-django-request-to-json-output)
65
67
-[Case 7. Add all Log Extra as Dictionary to the Standard Formatter (including Django log extra)](#case-7-add-all-log-extra-as-dictionary-to-the-standard-formatter-including-django-log-extra)
66
68
-[Case 8. Add Specific Log Extra to the Standard Formatter](#case-8-add-specific-log-extra-to-the-standard-formatter)
69
+
-[Case 9. Django add request info to all log records](#case-9-django-add-request-info-to-all-log-records)
**NOTE**: `LevelFilter` only support the special key `'()'` factory in the configuration file (it doesn't work with the normal `'class'` key).
622
625
626
+
## Django middleware request context
627
+
628
+
`AddToThreadContextMiddleware`is a [Middleware](https://docs.djangoproject.com/en/5.1/topics/http/middleware/) with which you can add the [Django](https://www.djangoproject.com/) [HttpRequest](https://docs.djangoproject.com/en/5.1/ref/request-response/#httprequest-objects) to thread local variables. The request object is added to a global variable in `logging_utilities.thread_context` and can be accessed in the following way:
629
+
630
+
```python
631
+
from logging_utilities.thread_context import thread_context
632
+
633
+
getattr(thread_context, 'request')
634
+
```
635
+
636
+
## Log thread context
637
+
638
+
`AddThreadContextFilter` provides a logging filter that will add data from the thread local store `logging_utilities.thread_context` to the log record. To set data on the thread store do the following:
639
+
640
+
```python
641
+
from logging_utilities.thread_context import thread_context
642
+
643
+
setattr(thread_context, 'key', data)
644
+
```
645
+
646
+
Configure the filter to decide which data should be added and how it should be named:
| `contexts` | list | empty | List of values to add to the log record. Dictionary must contain value for 'context_key' to read value from thread local variable. Dictionary must also contain 'logger_key' to set the value on the log record. |
660
+
623
661
## Basic Usage
624
662
625
663
### Case 1. Simple JSON Output
@@ -1279,6 +1317,57 @@ output:
1279
1317
2020-11-19 13:42:29,424 - DEBUG - your_logger - My log with extras - extra1=23
1280
1318
```
1281
1319
1320
+
### Case 9. Django add request info to all log records
1321
+
1322
+
Combine the use of the middleware `AddToThreadContextMiddleware` with the filters `AddThreadContextFilter` and `JsonDjangoRequest`, as well as the `JsonFormatter` to add request context to each log entry.
1323
+
1324
+
Activate the [middleware](https://docs.djangoproject.com/en/5.1/topics/http/middleware/#activating-middleware):
Configure the filters `AddThreadContextFilter` and `JsonDjangoRequest` to add the request from the thread variable to the log record and make it json encodable. Use the `JsonFormatter` to format the request values
0 commit comments