Logbook, pythonic logging - Remco WendtΒΆ

Tags: python, pun

(Summary of a talk at the Dutch Python meeting)

Armin Ronacher is the author or logbook. He’s now in a big discussion with Vinay Sajip, the author of logging in the python standard library. So what Remco says in this talk is his own opinion and his own experience.

Remco found the stdlib logging frustrating. Log rotation doesn’t really work nice. INI style configuration, yuck. And unit testing is hard. And so on. And logbook is much nicer. It is a modern logging replacement that’s actually compatible with stdlib’s logging.

The basic concepts are mostly the same as stdlib logging’s ones, like logrecord, logger, handler, formatter and filter. Extra is a processor, something that adds extra information to log records, like logged in users or remote IP address. A good thing about logbook is that you can safely use such extra information without the presence or absense of such an extra thing blowing up your entire logging system.

Logbook uses a lot of sensible defaults. By default, it logs to stderr. You don’t need to configure that beforehand. So it is easy to use for small scripts. When you do configure logbook, a handy feature is to have a different kind of logging when starting up an application and when actually running it (printing to the console in one case and logging to a window otherwise, for instance).

The compatibility with stdlib’s logging goes both ways. You can call a function to redirect logbook’s logging to stdlib’s logging. Or you can redirect stdlib’s logging to logbook.

Watch out with debug logging. Even if you don’t use it, it costs resources. You can configure logbook to really disable those debug logging statements, you cannot do this with stdlib’s logging.

Logbook also has some good email handling functionality. Mail can be send from a thread, so your main process doesn’t lock up. And you can throttle the amount of mails send to you within a certain period. So you don’t get 10000 similar messages in your inbox within five minutes.

Very handy: you can use the FingersCrossedHandler that omits debug messages unless there’s a warning or error. So the debug stuff is left out of the logfile if nothing is wrong. But if something blows up, you get a nice, detailed error log.

More niceness: sentry integration. Sentry is a central website where you can collect logging statements when something goes wrong. This is real handy in combination with the FingersCrossedHandler.

blog comments powered by Disqus
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):