"Thus, if a LOGGER is configured inside settings.py, we use that. Otherwise, we just use vanilla logging functions with the global logging configuration. Nice and sweet."
Naturally, the logger can be anything described [here](http://docs.python.org/library/logging.html), I'm just using the RotatingFileHandler as an example because that's what I was using in my project.
Full write up+shamless plug [here](http://mihasya.com/blog/?p=237)
When using Python's logging module in a concurrent environment (e.g. mod_python), messages get dropped by the standard file-based handlers. The [SocketHandler](http://www.python.org/doc/2.5.2/lib/node414.html) allows you to send logging messages to a remote socket. This snippet provides code for listening for such messages and writing them out to a log file. The final log file is configured as a standard logging file-based handler.
This sample site_logging.py module uses the [MODPYTHON Logging snippet](http://www.djangosnippets.org/snippets/627/). It assigns the ApacheLogHandler class to the main logging object, when Django is run inside MODPYTHON.
You must have the MODPYTHON Logging snippet, and name it modpython_logging.py for this to work. Apache will write to virtual host-specific logs only with Python 2.5.
Users are encouraged to change the two logging settings.
Log Level: handler.setLevel(logging.WARNING)
Log Format: logging.Formatter(...)
Activate this middleware and define `LOG_FORMAT` & `LOG_ROOTS` in your `settings.py`. Then you can use Python's `logging` module for easy logging within your application.
A MODPYTHON Apache Log Handler
This module provides a logging Handler class to a MODPYTHON Apache server. Python's standard [logging API](http://www.python.org/doc/2.5/lib/module-logging.html) explains how to [use a handler](http://www.python.org/doc/2.5/lib/multiple-destinations.html).
The handler, by default, writes entries to the Apache error_log using the standard Python logging API.
VIRTUAL HOSTS (Python 2.5)
This handler also supports Apache Virtual Hosts where the mp_server object is available. Then, it writes entries to the specific virtual-host server's error_log.
To get the mp_server object out of Django, you need the **log_extras()** function in your logging call (See the source comments).
This module must be bound to the Python logging API. Use a site_logging.py module to do that as this [related example](http://www.djangosnippets.org/snippets/960/) shows.
Reviewing some statistic-middleware-classes I think you might need one wich is working correct and high-performant.
Please comment it and have fun with it!
This is a simple Logging Middleware that uses the python logging functions.
Simply drop this snippet in a file in your project such as `logmw.py` (don't try to call it `logging.py` though), then add the class to MIDDLEWARE_CLASSES in your settings file. (for instance, `'mysite.logmw.LoggingMiddleware'`)
Updated 8/25/08: added PhonyLogger class that swallows log messages when logging is disabled, so code doesn't have to care if it's on or not (thanks to goodness for suggesting the idea, though I missed it before)
The solution is based on [dballanc's snippet](http://www.djangosnippets.org/snippets/420/).
Can easily be combined with any of the [SQL tracing solutions](http://www.djangosnippets.org/tags/debug/).
You might want to run a separate logging server and redirect your logs there. Please refer to the [logging reference manual](http://docs.python.org/lib/module-logging.html).
A handy ANSI-colored logging mechanism to display the SQL queries and times in the terminal when using django-admin.py runserver. DEBUG mode must be true for this to work.