MVorster
MVorster

Reputation: 13

CherryPy silence logging not working on Windows Service

Environment: Windows 7 64bit, Python version 3.2.5 with PyWin32-218 and cherrypy version 3.6.0.

I copied the windows service example (CP 3.1) from CherryPy as a Windows Service and found out that the service starts and stops almost immediately. After some fiddling I created a static log file to write to for debugging(because services run in diff directory than original script) and got this:

>[05/Feb/2015:16:29:05] ENGINE Bus STARTING
>[05/Feb/2015:16:29:05] ENGINE Error in 'start' listener   <cherrypy._cpchecker.Checker object at 0x0000000002556278>
>Traceback (most recent call last):
>  File "C:\Python32\lib\site-packages\cherrypy\process\wspbus.py", line 205, in publish
>    output.append(listener(*args, **kwargs))
>  File "C:\Python32\lib\site-packages\cherrypy\_cpchecker.py", line 39, in __call__
>    method()
>  File "C:\Python32\lib\site-packages\cherrypy\_cpchecker.py", line 176, in check_static_paths
>    % (msg, section, root, dir))
>  File "C:\Python32\lib\warnings.py", line 18, in showwarning
>    file.write(formatwarning(message, category, filename, lineno, line))
>AttributeError: 'NoneType' object has no attribute 'write'
>
>[05/Feb/2015:16:29:05] ENGINE Started monitor thread '_TimeoutMonitor'.
>[05/Feb/2015:16:29:05] ENGINE Serving on http://0.0.0.0:8080
>[05/Feb/2015:16:29:05] ENGINE Shutting down due to error in start listener:
>Traceback (most recent call last):
>  File "C:\Python32\lib\site-packages\cherrypy\process\wspbus.py", line 243, in start
>    self.publish('start')
>  File "C:\Python32\lib\site-packages\cherrypy\process\wspbus.py", line 223, in publish
>    raise exc
>cherrypy.process.wspbus.ChannelFailures: AttributeError("'NoneType' object has no attribute 'write'",)
>
>[05/Feb/2015:16:29:05] ENGINE Bus STOPPING
>[05/Feb/2015:16:29:06] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('0.0.0.0', 8080)) shut down
>[05/Feb/2015:16:29:06] ENGINE Stopped thread '_TimeoutMonitor'.
>[05/Feb/2015:16:29:06] ENGINE Bus STOPPED
>[05/Feb/2015:16:29:06] ENGINE Bus EXITING
>[05/Feb/2015:16:29:06] ENGINE Bus EXITED

Googled the error and found this running CherryPy 3.2 as a Windows service who had precisely the same error. Did some more research and saw that windows services' console output points to 'Nothing'. so I added sys.stdout = open('stdout.log', 'a+') and sys.stderr = open('stderr.log', 'a+') just before cherrypy.engine.start() and what do you know, it works!

BUT now I want CherryPy to not log at all. Tried various config settings and code eg.:log.screen = None and server.log_to_screen = False and checker.check_skipped_app_config = False and even

logger = cherrypy.log.access_log
logger.removeHandler(logger.handlers[0])

None of them work resulting in the service stopping and giving the same error as above.

Am I missing something or is it truly impossible to silence the logging?

Upvotes: 1

Views: 567

Answers (2)

cyraxjoe
cyraxjoe

Reputation: 5741

Follow the docs

Basically your config should looks like:

{'global': {
   'log.screen': False,
   'log.error_file': '',
   'log.access_file': ''
}}

Upvotes: 2

saaj
saaj

Reputation: 25234

I don't think it has much to do with CherryPy logging. As you can see your first stack trace ends in Python stdlib warnings module. Its purpose is letting developer know that some undesired conditions are met, but they aren't important enough to raise an exception. It also provides means of filtering warning messages. Here's the relevant quote from the module's documentation:

Warning messages are normally written to sys.stderr, but their disposition can be changed flexibly, from ignoring all warnings to turning them into exceptions. The disposition of warnings can vary based on the warning category (see below), the text of the warning message, and the source location where it is issued. Repetitions of a particular warning for the same source location are typically suppressed.

If you looked at the warnings.showwarning code, you would have seen this:

def showwarning(message, category, filename, lineno, file=None, line=None):
    """Hook to write a warning to a file; replace if you like."""
    if file is None:
        file = sys.stderr
    try:
        file.write(formatwarning(message, category, filename, lineno, line))
    except IOError:
        pass # the file (probably stderr) is invalid - this warning gets lost.

CherryPy actively uses warnings, and specifically in the config checker which is referenced in the first stack trace. It seems in your service environment sys.stderr is None. To completely avoid showing warnings, you can do the following:

import warnings
warnings.simplefilter('ignore')

Also if you would like to keep redirecting them, you may find this question useful.

Upvotes: 0

Related Questions