Logging to stdout and stderr

421 views
Skip to first unread message

doerw...@googlemail.com

unread,
Sep 2, 2008, 2:38:15 PM9/2/08
to cherrypy-users
Hello all!

I'm running CherryPy from supervisor, so supervisor handles log file
rotation for the CherryPy output. With CherryPy-3.1.0beta3 this worked
flawlessly: The access log got written to stdout and all errors and
custom logging got written to stderr. But now after updating to
CherryPy-3.1.0 everything seems to get logged to stderr. Here's the
script I use for testing this:

import os, optparse

import cherrypy

@cherrypy.expose
def search(phrase):
cherrypy.log(phrase)
return phrase

cherrypy.tree.mount(search, script_name="/search")


def main(args=None):
debug = 0
port = 8000
threads = 10

p = optparse.OptionParser(usage="usage: %prog [options]")
p.add_option("--debug", dest="debug", help="debug mode (0 or 1;
default %default)", type="int", default=debug)
p.add_option("--port", dest="port", help="port (default
%default)", type="int", default=port)
p.add_option("--threads", dest="threads", help="number of threads
(non-debug mode only; default %default)", type="int", default=threads)

(options, args) = p.parse_args(args)
if len(args) != 0:
p.error("incorrect number of arguments")
sys.exit(1)

configmap = {
"server.socket_host": "0.0.0.0",
"server.socket_port": options.port,
"server.screen": True,
}
if options.debug:
configmap["server.environment"] = "development"
else:
configmap["server.environment"] = "production"
configmap["server.thread_pool"] = options.threads

cherrypy.config.update(configmap)
cherrypy.engine.start()

if __name__ == "__main__":
main()

When I start this script with:

python foo.py >stdout.txt 2>stderr.txt

stdout.txt remains empty and stderr.txt contains all the logging
output:

[02/Sep/2008:20:27:04] ENGINE Started monitor thread
'_TimeoutMonitor'.
[02/Sep/2008:20:27:04] ENGINE Started monitor thread 'Autoreloader'.
[02/Sep/2008:20:27:04] ENGINE Serving on 0.0.0.0:8000
[02/Sep/2008:20:27:04] ENGINE Bus STARTED
[02/Sep/2008:20:27:16] foo
127.0.0.1 - - [02/Sep/2008:20:27:16] "GET /search/foo HTTP/1.1" 200 3
"" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1)
Gecko/2008070206 Firefox/3.0.1"

How can I get the old behaviour back (access log to stdout, everything
else to stderr)?

Servus,
Walter

Robert Brewer

unread,
Sep 28, 2008, 12:09:36 AM9/28/08
to cherryp...@googlegroups.com

Unfortunately, the easiest way is:

cherrypy.log._set_screen_handler(cherrypy.log.access_log, False)
cherrypy.log._set_screen_handler(cherrypy.log.access_log, True,
stream=sys.stdout)

If you would like the stream to be more easily configurable, or a
different default, please open a ticket and we'll discuss it there.


Robert Brewer
fuma...@aminus.org

doerw...@googlemail.com

unread,
Oct 20, 2008, 12:01:14 PM10/20/08
to cherrypy-users
On 28 Sep., 06:09, "Robert Brewer" <fuman...@aminus.org> wrote:
This workaround does indeed fix the problem.

> If you would like the stream to be more easily configurable, or a
> different default, please open a ticket and we'll discuss it there.

Done, see http://www.cherrypy.org/ticket/866

Servus,
Walter
Reply all
Reply to author
Forward
0 new messages