环境: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
用Google搜索错误,发现这个running CherryPy 3.2 as a Windows service的错误完全相同。
做了一些研究,看到了Windows服务&#39;控制台输出指向“什么都没有”。
所以我添加了sys.stdout = open('stdout.log', 'a+')
和sys.stderr = open('stderr.log', 'a+')
就在cherrypy.engine.start()
之前
你知道什么,它有效!
但现在我想要CherryPy根本不记录。
尝试了各种配置设置和代码,例如:log.screen = None
和server.log_to_screen = False
以及checker.check_skipped_app_config = False
甚至
logger = cherrypy.log.access_log
logger.removeHandler(logger.handlers[0])
它们都不起作用,导致服务停止并产生与上述相同的错误。
我是否遗漏了某些东西,或者真的无法让日志记录沉默?
答案 0 :(得分:1)
基本上,您的配置应如下所示:
{'global': {
'log.screen': False,
'log.error_file': '',
'log.access_file': ''
}}
答案 1 :(得分:0)
我不认为它与CherryPy日志记录有很大关系。正如您所看到的,您的第一个堆栈跟踪在Python stdlib warnings
模块中结束。它的目的是让开发人员知道某些不受欢迎的条件得到满足,但它们不足以引发异常。它还提供过滤警告消息的方法。以下是该模块文档中的相关引用:
警告消息通常写入sys.stderr,但可以灵活地更改其处置,从忽略所有警告到将其变为异常。警告的处置可能因警告类别(见下文),警告消息的文本以及发出警告消息的源位置而异。通常会抑制对同一源位置的特定警告的重复。
如果查看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.stderr
似乎是None
。要完全避免显示警告,您可以执行以下操作:
import warnings
warnings.simplefilter('ignore')
此外,如果您想继续重定向它们,您可能会发现this question有用。