CherryPy 静音日志记录不适用于 Windows 服务

CherryPy silence logging not working on Windows Service

环境:Windows 7 64 位,Python 版本 3.2.5,带有 PyWin32-218 和 cherrypy 版本 3.6.0。

我从 CherryPy as a Windows Service 复制了 windows 服务示例 (CP 3.1),发现该服务几乎立即启动和停止。经过一些摆弄后,我创建了一个静态日志文件来写入调试(因为 diff 目录中的服务 运行 与原始脚本不同)并得到了这个:

>[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

用谷歌搜索错误,发现这个 running CherryPy 3.2 as a Windows service 犯了完全相同的错误。 做了更多研究,发现 windows 服务的控制台输出指向 'Nothing'。 所以我添加了 sys.stdout = open('stdout.log', 'a+')sys.stderr = open('stderr.log', 'a+') 就在 cherrypy.engine.start() 之前 你知道吗,它有效!

但现在我希望 CherryPy 根本不登录。 尝试了各种配置设置和代码,例如:log.screen = Noneserver.log_to_screen = Falsechecker.check_skipped_app_config = False 甚至

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

None 其中的工作导致服务停止并给出与上述相同的错误。

我是不是遗漏了什么,或者真的不可能让日志静音?

我认为这与 CherryPy 日志记录没有太大关系。如您所见,您的第一个堆栈跟踪以 Python stdlib warnings 模块结束。它的目的是让开发人员知道满足了一些不需要的条件,但它们还没有重要到引发异常。它还提供过滤警告消息的方法。这是模块文档中的相关引述:

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.

如果您查看 warnings.showwarning 代码,您会看到:

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 积极使用 warnings,特别是在第一个堆栈跟踪中引用的配置检查器中。看来在你的服务环境中 sys.stderrNone。要完全避免显示警告,您可以执行以下操作:

import warnings
warnings.simplefilter('ignore')

此外,如果您想继续重定向它们,您可能会发现 this question 很有用。

Follow the docs

基本上你的配置应该是这样的:

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