2015-02-05 110 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