2012-01-05 25 views
2

我一直在使用Python記錄模塊相當長的一段時間,但最近,測試與老的Python版本的兼容性,當我遇到了一些麻煩:變化記錄,因爲2.5版

Traceback (most recent call last): 
    File "hunter.py", line 16, in <module> 
    logging.config.fileConfig("logging.conf") 
    File "/usr/lib/python2.5/logging/config.py", line 85, in fileConfig 
    _install_loggers(cp, handlers) 
    File "/usr/lib/python2.5/logging/config.py", line 229, in _install_loggers 
    logger.addHandler(handlers[string.strip(hand)]) 
KeyError: 'hunterFileHandler' 

與以下配置文件(只重要的部分):

[loggers] 
keys=root,hunter 

[handlers] 
keys=consoleHandler, hunterFileHandler 

[formatters] 
keys=simpleFormatter 

[logger_root] 
level=DEBUG 
handlers=consoleHandler 

[logger_hunter] 
level=DEBUG 
handlers=consoleHandler, hunterFileHandler 
qualname=hunter 
propagate=0 

[handler_consoleHandler] 
class=StreamHandler 
level=DEBUG 
formatter=simpleFormatter 
args=(sys.stdout,) 

[handler_hunterFileHandler] 
class=handlers.RotatingFileHandler 
level=DEBUG 
formatter=simpleFormatter 
args=("logs/hunter.log", "a", 0, 10,) 

[formatter_simpleFormatter] 
format=%(asctime)s:[%(name)s][%(levelname)5s] %(message)s 
datefmt= 

有一個簡單的方法,使不重複大部分配置向後兼容?

+1

看起來像這[問題](http://stackoverflow.com/q/1018527/183066)可能對您的問題有用。 – jcollado 2012-01-05 22:01:12

回答

0

你的問題很可能是在這條線的空間造成的:

keys=consoleHandler, hunterFileHandler 
        ^

由於2.5中的錯誤,導致空間不跳,你會得到一個KeyError因爲密鑰被錯誤地設置爲" hunterFileHandler"。如果您將此行更改爲

keys=consoleHandler,hunterFileHandler 

然後文件應該正確加載。

但是,你應該注意到在2.5和2.6中修復了幾個bug,所以你可能會被其中一個(除了我提到的)之外的其中一個所咬住。查看jcollado對您問題的評論。