Monday 13 September 2010

Using filters to add contextual information to logs

As an alternative to using LoggerAdapters to add contextual information to logs, you can also use Filters to achieve the same result. This approach has been documented with an example in the in-development Python documentation, here. This functionality has been available since the earliest version of Python logging (i.e. since 2.3) but since the advent of thread-locals in 2.4 it can be used to log thread-local information. This can be used, for example, in an HTTP request handler, to record request-specific information such as client IP address, HTTP method, path, query string etc. in your logs.

No comments:

Post a Comment