2015-02-05 71 views
1

环境:Windows 7 64bit,Python版本3.2.5,使用PyWin32-218和cherrypy 3.6.0版。CherryPy沉默日志记录在Windows服务上不起作用

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

>[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服务的控制台输出指向'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]) 

他们没有工作。

我是否错过了一些东西,或者真的不可能让记录沉默?

回答

1

Follow the docs

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

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

非常感谢!就像一个白痴,我在文档搜索,但在错误的部分和搜索条件。 – MVorster 2015-02-06 06:39:48

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.stderrNone。为了完全避免显示警告,可以做到以下几点:

import warnings 
warnings.simplefilter('ignore') 

此外,如果你想保持对它们进行重定向,你可能会发现this question有用。

+0

这个工作100%,但并没有 '禁用' 的记录。不要误解我CherryPy在添加完成后完美无缺地工作。谢谢:) – MVorster 2015-02-06 06:35:55