jvandal / modwsgi

Automatically exported from code.google.com/p/modwsgi
0 stars 0 forks source link

Should perhaps call logging.basicConfig() and bind logger output to __stderr__. #138

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Because stderr is replaced with special wrapper that redirects output to Apache 
error log via Apache 
API functions for logging, each line gets prefixed with date/time and Apache 
'ERR' debug level tag. 
When logging module is used on top of that, you get duplication of separate 
Python internal debug 
level tag. Going via the mod_wsgi stderr wrapper, it is affected by limitations 
in that in respect of 
embedded nulls and line lengths.

What would possibly be better is that mod_wsgi invokes logging.basicConfig() 
with stream set to 
sys.__stderr__ so that stderr wrapper bypassed. This should be okay because 
logging module 
should flush after each message ensuring that messages actually get to log, 
given that __stderr__ is 
separately buffered. The message prefix format could also be set using format 
and datefmt to 
match Apache output.

A directive could also be supplied called WSGIPythonLogLevel which if not 
specified will instead 
default to whatever log level is used by Apache through the LogLevel directive. 
If 
WSGIPythonLogLevel is used, would apply to all interpreters. If specific 
applications needed to 
override it for an interpreter, they would need to call logging.root.setLevel().

The only drawback of all these changes is that any logged messages bypass 
Apache hook functions 
for logging which allow a Apache module to intercept them. This isn't seen as a 
problem at the 
moment. Also, the output from the Python logging system would never be passed 
through syslog if 
that is where Apache was configured to send it.

In case all this does cause problems, should have a directive which disables 
setting all this up. This 
could be an overloading on WSGIPythonLogLevel, such that if set to '-' would 
disable it, or instead 
use a separate directive such as WSGIPythonLogging with On/Off value.

Original issue reported on code.google.com by Graham.Dumpleton@gmail.com on 10 Mar 2009 at 4:00

GoogleCodeExporter commented 8 years ago
For example, when 'logging' is used without even calling logging.basicConfig() 
the output is:

[Tue Mar 10 16:41:04 2009] [error] WARNING:root:This is a warning message
[Tue Mar 10 16:41:04 2009] [error] ERROR:root:This is an error message
[Tue Mar 10 16:41:04 2009] [error] CRITICAL:root:This is a critical error 
message

One problem with implementing this integration is that the logging module API 
is different for older versions. 
Specifically, the logging.basicConfig() option takes no arguments in Python 
2.3. This means wouldn't be able to 
do it for older version of Python.

Original comment by Graham.Dumpleton@gmail.com on 10 Mar 2009 at 5:46

GoogleCodeExporter commented 8 years ago
Where basicConfig() does take arguments, then can use:

import logging
import sys

logging.basicConfig(stream=sys.__stderr__, datefmt='%a %b %e %H:%M:%S %Y',
        format='[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s')

This would yield:

[Tue Mar 10 17:14:36 2009] [notice] caught SIGTERM, shutting down
[Tue Mar 10 17:14:38 2009] [notice] Apache/2.2.9 (Ubuntu) mod_wsgi/3.0-TRUNK
Python/2.5.2 configured -- resuming normal operations
[Tue Mar 10 17:14:43 2009] [WARNING] [root] This is a warning message
[Tue Mar 10 17:14:43 2009] [ERROR] [root] This is an error message
[Tue Mar 10 17:14:43 2009] [CRITICAL] [root] This is a critical error message

This produced by Python logging module have uppercase log level name and have 
the
name of the logger channel also displayed.

If also do:

logging.root.setLevel(logging.DEBUG)

then get higher levels of debug.

[Tue Mar 10 17:18:36 2009] [DEBUG] [root] This is a debug message
[Tue Mar 10 17:18:36 2009] [INFO] [root] This is an info message
[Tue Mar 10 17:18:36 2009] [WARNING] [root] This is a warning message
[Tue Mar 10 17:18:36 2009] [ERROR] [root] This is an error message
[Tue Mar 10 17:18:36 2009] [CRITICAL] [root] This is a critical error message

Anyway, given differences in Python versions, perhaps best not to do this and 
just
document in wiki instead how to better integrate logging module with mod_wsgi.

Original comment by Graham.Dumpleton@gmail.com on 10 Mar 2009 at 6:20

GoogleCodeExporter commented 8 years ago
Decided not to take any action on this.

Original comment by Graham.Dumpleton@gmail.com on 12 Mar 2009 at 5:03