Opened 7 years ago

Closed 4 years ago

Last modified 4 years ago

#5677 closed (fixed)

Debugging with mod_python (apache)

Reported by: Manfred Wassmann <manolo@…> Owned by: nickefford
Component: Documentation Version: master
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: UI/UX:

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)

modpython.diff (1.9 KB) - added by nickefford 7 years ago.
Patch to modpython.txt to clarify use of print statements
5677.diff (1.9 KB) - added by timo 5 years ago.
updated patch, including new location of modpython.txt
5677_modpython.diff (1.8 KB) - added by danielr 5 years ago.
Updated patch removing references to WSGI.
5677.2.diff (1.5 KB) - added by timo 4 years ago.
updated danielr's patch to apply cleanly trunk

Download all attachments as: .zip

Change History (23)

comment:1 Changed 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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.

comment:2 Changed 7 years ago by grahamd

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 Changed 7 years ago by nickefford

  • Owner changed from nobody to nickefford

comment:4 Changed 7 years ago by nickefford

  • Has patch set
  • Keywords sprintdec01 added

Done, incorporating suggestions of Malcolm and grahamd.

Changed 7 years ago by nickefford

Patch to modpython.txt to clarify use of print statements

comment:5 Changed 7 years ago by Simon G <dev@…>

  • Triage Stage changed from Accepted to Ready for checkin

comment:6 follow-up: Changed 7 years ago by dharris

What about using standard python logging? Is there a way to get this to go to the error log (or elsewhere)?

comment:7 in reply to: ↑ 6 Changed 7 years ago by adroffner@…

  • Component changed from Documentation to Contrib apps
  • Has patch unset
  • Keywords logging modpython mod_python added; sprintdec01 removed
  • Needs documentation set
  • Summary changed from Debugging with mod_python (apache) improperly documented to Logging errors with mod_python (apache)
  • Triage Stage changed from Ready for checkin to 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 Changed 7 years ago by anonymous

  • Component changed from Contrib apps to Documentation
  • Has patch set
  • Keywords logging modpython removed
  • Needs documentation unset
  • Summary changed from Logging errors with mod_python (apache) to Debugging with mod_python (apache)
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:9 Changed 7 years ago by ubernostrum

  • Triage Stage changed from Ready for checkin to 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 Changed 7 years ago by guettli

  • Triage Stage changed from Unreviewed to Accepted

This ticket was already accepted (Triage Stage).

comment:11 Changed 7 years ago by Camille Harang

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)

Changed 5 years ago by timo

updated patch, including new location of modpython.txt

comment:12 Changed 5 years ago by timo

  • Triage Stage changed from Accepted to 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 Changed 5 years ago by kmtracey

  • Triage Stage changed from Ready for checkin to 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 Changed 5 years ago by Rupe

  • milestone set to 1.2

comment:15 Changed 5 years ago by russellm

  • milestone 1.2 deleted

Deferring due to the absence of a trunk-ready patch.

Changed 5 years ago by danielr

Updated patch removing references to WSGI.

comment:16 Changed 5 years ago by danielr

Updated patch removing references to WSGI.

Changed 4 years ago by timo

updated danielr's patch to apply cleanly trunk

comment:17 Changed 4 years ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

comment:18 Changed 4 years ago by DrMeers

  • Resolution set to fixed
  • Status changed from new to closed

(In [14059]) Fixed #5677 -- update modpython stdout documentation. Thanks to Manfred Wassmann for the report, nickefford for the initial patch and Graham Dumpleton for the expert advice.

comment:19 Changed 4 years ago by DrMeers

(In [14060]) [1.2.X] Fixed #5677 -- update modpython stdout documentation. Thanks to Manfred Wassmann for the report, nickefford for the initial patch and Graham Dumpleton for the expert advice.

Backported of r14059 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top