The more complex are my web projects, the more I realize the importance of a good logging strategy.

Working with Python and Django, I have access to excellent tools to log everything I want.

The python's standard logging module is really complete and functional, and gives you a bunch of loggers, handlers, filters, formatters and so on to log whatever you want wherever you want however you want.

The django's logging integration is very efficient and well documented.

Sentry is a log server that can catch and centralize your logs, errors and exceptions, and gives you great insights about your web app errors. Plus sentry's integration into django is excellent.

Problem is the documentation about logging is somewhat unclear. Well, rather than unclear, I should said incomplete. HOW to log is well documented, what's missing is info about a real logging strategy. WHAT, WHERE, WHEN should you log?

What's in a good logging strategy? Logging information can serve different purposes. Here are a few use cases taken from Arrosware, my current side project : When a user encounters a critical error, I want to be warned immediatly with an email. I should also be able to trace the source of the error, and retrieve all the needed data to reproduce it.

When a new account is created, I want to be warned immediatly, so I can check that everything is alright. I don't really need additional informations, though.

When a user tops up his sms account, I want that information to be stored, so I can retrieve it just in case something would go wrong. But I don't want to pollute my mailbox with that information.

Debug messages that I put into my code should appear in my development's environment, but must be discarded on my production server. Now that I have my use cases, I can define specific actions depending on the log level : DEBUG : Display the log on the console if in development environment, discard otherwise.

: Display the log on the console if in development environment, discard otherwise. INFO : Send the log to syslog.

: Send the log to syslog. WARNING : Send the log to my mailbox.

: Send the log to my mailbox. ERROR: Send the log to sentry. Obviously, actions must be cascading. An error message must be sent to sentry AND to my mailbox AND… Now that we defined precisely our logging strategy, let's have a look at the technical implementation.