From 0a43fe0e4468c18bdc59d212736c16702b17b5d5 Mon Sep 17 00:00:00 2001 From: Trevor Bortins Date: Wed, 5 Feb 2020 17:02:35 -0800 Subject: [PATCH] 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! --- README.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 35fd448..ca7d3aa 100644 --- a/README.rst +++ b/README.rst @@ -104,12 +104,21 @@ To do so you should override ``JSONFormatter.json_record()``. .. code-block:: python class CustomisedJSONFormatter(json_log_formatter.JSONFormatter): - def json_record(self, message, extra, record): + def json_record(self, message: str, extra: dict, record: logging.LogRecord) -> dict: extra['message'] = message extra['user_id'] = current_user_id() extra['ip'] = current_ip() + + # Include builtins + extra['level'] = record.levelname + extra['name'] = record.name + if 'time' not in extra: extra['time'] = django.utils.timezone.now() + + if record.exc_info: + extra['exc_info'] = self.formatException(record.exc_info) + return extra Let's say you want ``datetime`` to be serialized as timestamp.