Skip to content

Commit

Permalink
Add builtins example to README
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
wulftone authored Feb 6, 2020
1 parent f078b11 commit 0a43fe0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0a43fe0

Please sign in to comment.