Sunday, January 26, 2020

Will McGugan: Prettier logging with Rich

© 2020 Will McGugan

Pretty log output

If you are a Python developer you may spend a large part of your day reading log output that looks like this:

© 2020 Will McGugan

Ugly log output

Front end tools like the Chrome developer console have far superior rendering for logs, but in the back-end we're stuck with an ancient technology that is older than most of the readers of this blog. I'm talking of course of the terminal which is likely to be the primary interface to back-end development until I retire.

Still I think we can do a little better. I wanted to use my terminal rendering library, Rich to render log output that is easier on the eye. The results are promising:

© 2020 Will McGugan

Pretty log output

There are a few things going on here. Important fields are rendered in their own column to make it easier to scan. To reduce visual clutter, the time field is only rendered if it changes and I've set the date format to time only, which is fine for local development (if you forget what day it is you need a vacation). The message column has some syntax highlighting applied to it, tuned for web development, but more importantly it is word-wrapped. Finally there is a column for the python file and line that called the log method.

This would be my ideal logging for web-development, your mileage may vary and you may want to tune it for your domain.

You can try this yourself by configuring logging to use rich.logging.RichHandler.

Rich is still in active development and not ready for a 1.0 release. If you want to use it anger, best pin the current version for now.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...