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
-[Filter out LogRecord attributes based on their types](#filter-out-logrecord-attributes-based-on-their-types)
48
+
-[Attribute Type Filter Constructor](#attribute-type-filter-constructor)
49
+
-[Attribute Type Filter Config Example](#attribute-type-filter-config-example)
47
50
-[ISO Time with Timezone](#iso-time-with-timezone)
48
51
-[ISO Time Filter Constructor](#iso-time-filter-constructor)
49
52
-[ISO Time Config Example](#iso-time-config-example)
@@ -62,6 +65,7 @@ All features can be fully configured from the configuration file.
62
65
-[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)
63
66
-[Case 8. Add Specific Log Extra to the Standard Formatter](#case-8-add-specific-log-extra-to-the-standard-formatter)
If you want to log the [Django](https://www.djangoproject.com/) [HttpRequest](https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects) object using the [JSON Formatter](#json-formatter), this filter is for made for you. It converts the `record.request` attribute to a valid json object or a string if the attribute is not an `HttpRequest` instance. It is also useful when using Django with the JSON Formatter because Django adds in some of its logs either an HttpRequest object to the log extra or a socket object.
467
+
If you want to log the [Django](https://www.djangoproject.com/) [HttpRequest](https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects) object using the [JSON Formatter](#json-formatter), this filter is for made for you. It converts the `record.http_request` attribute (or the attribute specified by `attr_key` in the constructor) to a valid json object if it is of type `HttpRequest`.
464
468
465
469
The `HttpRequest` attributes that are converted can be configured using the `include_keys` and/or `exclude_keys` filter parameters. This can be useful if you want to limit the log data, for example if you don't want to log Authentication headers.
466
470
471
+
:warning: The django framework adds sometimes an HttpRequest or socket object under `record.request` when
472
+
logging. So if you decide to use the attribute name `request` for this filter, beware that you
473
+
will need to handle the case where the attribute is of type `socket` separately, for example by
474
+
filtering it out using the attribute type filter. (see example [Filter out LogRecord attributes based on their types](#filter-out-logrecord-attributes-based-on-their-types))
475
+
467
476
### Usage
468
477
469
478
Add the filter to the log handler and then add simply the `HttpRequest` to the log extra as follow:
| `include_keys` | list | None | All request attributes that match any of the dotted keys of the list will be jsonify in the `record.request`. When `None` then all attributes are added (default behavior). |
480
-
| `exclude_keys` | list | None | All request attributes that match any of the dotted keys of the list will not be added to the jsonify of the `record.request`. **NOTE** this has precedence to `include_keys` which means that if a key is in both list, then it is not added. |
488
+
| `include_keys` | list | None | All request attributes that match any of the dotted keys of the list will be added to the jsonifiable object. When `None` then all attributes are added (default behavior). |
489
+
| `exclude_keys` | list | None | All request attributes that match any of the dotted keys of the list will not be added to the jsonifiable object. **NOTE** this has precedence to `include_keys` which means that if a key is in both lists, then it is not added. |
490
+
| `attr_key` | str | `http_request` | The name of the attribute that stores the HttpRequest object. It will be replaced in place by a jsonifiable dict representing this object. (Note that django sometimes stores an `HttpRequest` under `attr_key: request`. This is however not the default as django also stores other types of objects under this attribute name.)
|`typecheck_list` | dict(key, type\|list of types)| None | A dictionary that maps keys to a type or a list of types. By default, it will only keep a parameter matching a key if the types match or if any of the types in the list match (white list). If in black list mode, it will only keep a parameter if the types don't match. Parameters not appearing in the dict will be ignored and passed though regardless of the mode (whitelist or blacklist).
521
+
| `is_blacklist` | bool | false | Whether the list passed should be a blacklist or a whitelist. To use both, simply include this filter two times, one time with this parameter set true and one time with this parameter set false.
is_blacklist: False # Default value is false, so this could be left out
530
+
typecheck_list:
531
+
# For each attribute listed, one type or a list of types can be specified
532
+
request: # can only be a toplevel attribute (no dotted keys allowed)
533
+
- django.http.request.HttpRequest # Can be a class name only or the full dotted path
534
+
- builtins.dict
535
+
my_attr: myClass
536
+
```
537
+
499
538
## ISO Time with Timezone
500
539
501
540
The standard logging doesn't support the time as ISO with timezone; `YYYY-MM-DDThh:mm:ss.sss±hh:mm`. By default `asctime` uses a ISO like format; `YYYY-MM-DD hh:mm:ss.sss`, but without `T` separator (although this one could be configured by overriding a global variable, this can't be done by config file). You can use the `datefmt` option to specify another date format, however this one don't supports milliseconds, so you could achieve this format: `YYYY-MM-DDThh:mm:ss±hh:mm`.
From version 3.x.x to version 4.x.x there is the following breaking change:
1287
+
1288
+
- The django request filter by default now reads the attribute `record.http_request` instead of
1289
+
the attribute `record.request`. There is however a new option `attr_name` in the filters constructor
1290
+
to manually specify the attribute name. See the example [Add parts of Django Request to JSON Output](#case-6-add-parts-of-django-request-to-json-output) for an example on how to use `attr_name` to be
1291
+
backward-compatible with 3.x.x
1292
+
1224
1293
### Version 3.x.x Breaking Changes
1225
1294
1226
1295
From version 2.x.x to version 3.x.x there is the following breaking change:
0 commit comments