In Django, how do I allow print statements to work with Apache WSGI?

前端 未结 4 963
一生所求
一生所求 2020-12-11 18:46

\"print\" only works in development server. But what if I want it to work in Apache? Just in case I forget to comment it out...I want to be able to go smoothly without causi

相关标签:
4条回答
  • 2020-12-11 19:13

    As for quick print, just can just use:

    print >>sys.stderr, 'log msg'
    

    -- then it lands in error.log, of course.

    0 讨论(0)
  • 2020-12-11 19:13

    What you're proposing is a Bad Idea, but if you insist on doing it anyways, check out the mod_wsgi configuration directives:

    WSGIRestrictStdout

    Description: Enable restrictions on use of STDOUT.
    Syntax:      WSGIRestrictStdout On|Off
    Default:     WSGIRestrictStdout On
    Context:     server config
    Module:      mod_wsgi.c
    

    A well behaved Python WSGI application should never attempt to write any data directly to sys.stdout or use the print statement without directing it to an alternate file object. This is because ways of hosting WSGI applications such as CGI use standard output as the mechanism for sending the content of a response back to the web server. If a WSGI application were to directly write to sys.stdout it could interfere with the operation of the WSGI adapter and result in corruption of the output stream.

    In the interests of promoting portability of WSGI applications, mod_wsgi restricts access to sys.stdout and will raise an exception if an attempt is made to use sys.stdout explicitly.

    The only time that one might want to remove this restriction is purely out of convencience of being able to use the print statement during debugging of an application, or if some third party module or WSGI application was errornously using print when it shouldn't. If restrictions on using sys.stdout are removed, any data written to it will instead be sent through to sys.stderr and will appear in the Apache error log file.

    0 讨论(0)
  • 2020-12-11 19:21

    If you want to write print statements to Apache error log, you can use sys.stderr:

    import sys
    sys.stderr.write('log mgs')
    

    then it will be in apache error log file.

    0 讨论(0)
  • 2020-12-11 19:31

    See Graham Dumpleton's post:

    • WSGI and printing to standard output
    0 讨论(0)
提交回复
热议问题