June 15, 2020 will

Richer Django logging

I recently tested Rich logging with a Django project and the results were good. Here's what it looks like:

A Django app with Rich logging

Note the file and line at the right hand of the terminal which shows were the log function was called. If your terminal supports hyperlinks, you can click that link to open the file!

For reference, here's what it looks like without Rich logging.

A Django app without Rich logging

This is the default RichHandler class in Rich. It would be possible to do a little more customization for Django logs. Maybe highlight 5xx in red, for example.

To add this to your Django project, pip install rich then configure the console to use "rich.logging.RichHandler" rather than StreamHandler.

    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {"rich": {"datefmt": "[%X]"}},
    "handlers": {
        "console": {
            "class": "rich.logging.RichHandler",
            "formatter": "rich",
            "level": "DEBUG",
    "loggers": {"django": {"handlers": ["console"]}},

The process is much the same for any project. See the documentation for the Rich logging handler for details.

Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
Taulant Aliraj

Hi, this is a great library. What about tracebacks, would it be possible te let rich handle those too? If so, how would you configure it? I have seen the install method in the documentation, but i can't figure out how tu use it in a django project.

Will McGugan

I don't think so I'm afraid. Django captures the traceback to generate the 500, so Rich never has the opportunity to render them. I don't know of any straight forward way of integrating Rich tracebacks with Django, without modifying Django itself.

Beto Dealmeida

This is super cool! I just added Rich to my blog engine, and it was such a small change for a so much nicer experience! I love it, thanks for making it!

Will McGugan

Glad you like it. Good to see someone else who wrote their own blog software! 👍