Skip to content

Commit 0a43fe0

Browse files
authored
Add builtins example to README
There seem to be a bunch of questions about how to do this and people keep saying "I figured it out" without telling anyone else how they did it! This is how you can do it, in the README!
1 parent f078b11 commit 0a43fe0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

README.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,21 @@ To do so you should override ``JSONFormatter.json_record()``.
104104
.. code-block:: python
105105
106106
class CustomisedJSONFormatter(json_log_formatter.JSONFormatter):
107-
def json_record(self, message, extra, record):
107+
def json_record(self, message: str, extra: dict, record: logging.LogRecord) -> dict:
108108
extra['message'] = message
109109
extra['user_id'] = current_user_id()
110110
extra['ip'] = current_ip()
111+
112+
# Include builtins
113+
extra['level'] = record.levelname
114+
extra['name'] = record.name
115+
111116
if 'time' not in extra:
112117
extra['time'] = django.utils.timezone.now()
118+
119+
if record.exc_info:
120+
extra['exc_info'] = self.formatException(record.exc_info)
121+
113122
return extra
114123
115124
Let's say you want ``datetime`` to be serialized as timestamp.

0 commit comments

Comments
 (0)