#5677 closed (fixed)
Debugging with mod_python (apache)
Reported by: | Owned by: | Nick Efford | |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Keywords: | mod_python | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is what the docs say:
If you’re the type of programmer who debugs using scattered print statements, note that print statements have no effect in mod_python; they don’t appear in the Apache log, as one might expect. If you have the need to print debugging information in a mod_python setup, either do this: assert False, the_value_i_want_to_see Or add the debugging information to the template of your page.
This is not completely true. Everything you write to sys.stderr ends up in the apache error log, like this
import sys sys.stderr.write('This goes to the apache error log')
Attachments (4)
Change History (23)
comment:1 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 17 years ago
If talking about use of 'print', can you strongly discourage use of 'print' without explicitly directing the output to sys.stderr. Ie., discourage use of:
print 'debug text'
This is because the resulting code is technically not safe to run on all possible WSGI hosting solutions. This is because some WSGI hosting solutions, such as CGI, use sys.stdout to communicate with the core web server. Thus, if users use 'print' without directing it anywhere, the output from it will interleave with valid output being sent back to the client and screw up the response.
So, suggest use of:
print >> sys.stderr, ' debug text'
if anything.
But then as already pointed out, in mod_python this really needs to be:
print >> sys.stderr, ' debug text' sys.stderr.flush()
What Django developers should perhaps instead consider to get around this, is in the Django mod_python adapter when it is imported, replace sys.stderr with a stream like object which buffers data until an EOL is encountered and then call mod_python.apache.log_error() explicitly. This will ensure that when sys.stderr is used that output appears promptly, it will also attach time stamps to each line of output. Note that if the output passed to the dummy stream object contains embedded EOL, then the line should be split and passed to log_error() one line at a time though.
By doing this sys.stderr replacement, it would avoid all these problems with output not appearing in Apache log in a timely manner. Although one could replace sys.stdout as well, as I said you probably want to discourage people outputing to sys.stdout directly or indirectly.
comment:3 by , 17 years ago
Owner: | changed from | to
---|
comment:4 by , 17 years ago
Has patch: | set |
---|---|
Keywords: | sprintdec01 added |
Done, incorporating suggestions of Malcolm and grahamd.
by , 17 years ago
Attachment: | modpython.diff added |
---|
Patch to modpython.txt to clarify use of print statements
comment:5 by , 17 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
follow-up: 7 comment:6 by , 17 years ago
What about using standard python logging? Is there a way to get this to go to the error log (or elsewhere)?
comment:7 by , 17 years ago
Component: | Documentation → Contrib apps |
---|---|
Has patch: | unset |
Keywords: | logging modpython mod_python added; sprintdec01 removed |
Needs documentation: | set |
Summary: | Debugging with mod_python (apache) improperly documented → Logging errors with mod_python (apache) |
Triage Stage: | Ready for checkin → Unreviewed |
Replying to dharris:
What about using standard python logging? Is there a way to get this to go to the error log (or elsewhere)?
I wrote a python logging Handler class, ApacheLogHandler, to do just that. It even supports Apache virtual hosts. Use it the same way as the StreamHandler 'console' object in the Python logging API docs.
I'm trying to make this a clean django.contrib.logging.* feature. Right now it's a Django Snippet.
comment:8 by , 17 years ago
Component: | Contrib apps → Documentation |
---|---|
Has patch: | set |
Keywords: | logging modpython removed |
Needs documentation: | unset |
Summary: | Logging errors with mod_python (apache) → Debugging with mod_python (apache) |
Triage Stage: | Unreviewed → Ready for checkin |
comment:9 by , 17 years ago
Triage Stage: | Ready for checkin → Unreviewed |
---|
Please don't anonymously bump something to "ready for checkin"; that status implies a certain level of review by an experienced triager.
comment:10 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
This ticket was already accepted (Triage Stage).
comment:11 by , 17 years ago
Another quick solution to get direct output is to print into a tty. Get the tty device by typing 'tty' in a terminal (here /dev/pts/5), and use the small function bellow to print what you need to see:
def print_tty(message, dev='/dev/pts/5'): tty = open(dev, 'w') tty.write('%s' % message) tty.flush()
Example:
print_tty(request.POST)
by , 15 years ago
updated patch, including new location of modpython.txt
comment:12 by , 15 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I don't think we can expect to document every possible solution to debugging, but the current patch looks like a good addition.
comment:13 by , 15 years ago
Triage Stage: | Ready for checkin → Accepted |
---|
I do not like the current patch's inclusion of a vague reference to corrupting client output in "some WSGI hosting solutions". This is text going into the page describing mod_python deployment, specifically. It should limit itself to what happens under mod_python. Sure, adding prints to stdout is a bad idea in other hosting environments too, but that's irrelevant to someone trying to figure out how to debug a problem under mod_python and more likely to cause confusion than anything else.
comment:14 by , 15 years ago
milestone: | → 1.2 |
---|
comment:17 by , 14 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:18 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
If somebody is going to create a patch for this, it must also mention that stderr is buffered in mod_python, so flushing the pipe each time is a necessity if you want to actually see the results in more-or-less realtime.