Richer Django logging
I recently tested Rich logging with a Django project and the results were good. Here's what it looks like:
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.
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.
LOGGING = {
"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.
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.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.
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!
Glad you like it. Good to see someone else who wrote their own blog software! 👍
You can do the following:
Any extra param in the handler config is passed to the handler constructor. And then the RichHandler will deal with the
sys.exc_info()
object.