2013-10-22 18 views
0

我想我錯過了一些大事,對於我的生活,我無法弄清楚。我有一個logging.conf文件,我正在嘗試使用我的主文件(例如,xyz.py)文件進行讀取。但我得到這個奇怪的錯誤。我有以下跟蹤回溯配置文件 - logging.conf,然後在xyz.py相關部分。涉及.conf + main.py模塊的日誌記錄錯誤

File "xyz.py", line 28, in <module> 
log = logging.config.fileConfig('/Users/Username/Desktop/logging.conf') 
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 78, in fileConfig 
handlers = _install_handlers(cp, formatters) 
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 156, in _install_handlers 
h = klass(*args) 
TypeError: __init__() takes at most 7 arguments (23 given) 

配置文件 - 全路徑= /Users/Username/Desktop/logging.config(I,接着從http://docs.python.org/release/2.5.2/lib/logging-config-fileformat.html指令)

[loggers] 
keys=root 

[handlers] 
keys=handlersmtp, handlerfile 

[formatters] 
keys=formatter 

[formatter_formatter] 
format=%(asctime)s %(name)s %(levelname)s %(message)s 
datefmt= 
class=logging.Formatter 

[logger_root] 
level=NOTSET 
handlers=handlersmtp, handlerfile 

[handler_handlersmtp] 
class=handlers.SMTPHandler 
level= INFO 
formatter=formatter 
args=(('localhost', 25),'[email protected]', ['[email protected]'], 
         'The log') 


[handler_handlerfile] 
class=handlers.RotatingFileHandler 
level= INFO 
formatter=formatter 
backupCount=1440 
args=('alogger.log') 

在主文件中的部分 - xyz.py

import logging 
import logging.config 

log = logging.config.fileConfig('/Users/Username/Desktop/logging.config') 

我看着Python是logging/config.py模塊,但無法跟蹤爲什麼它提高了這一點。這是一個非常大的文件。

編輯:

@ VineySajip的答案去掉上面的錯誤,但現在我在做這個新的。

[handler_handlerfile] 
class=handlers.RotatingFileHandler 
level= INFO 
formatter=formatter 
args=('alogger.log', mode='a', maxBytes=25000, 
backupCount=0, encoding=None, delay=0) #New line to fit 
        #this page but code has it all in 1 line 

新的回溯:

Traceback (most recent call last): 
    File "cpu6.py", line 29, in <module> 
    log = logging.config.fileConfig('/Users/Username/Desktop/logging.ini') 
    File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/ 
    Versions/2.7/lib/python2.7/logging/config.py", line 78, in fileConfig 
    handlers = _install_handlers(cp, formatters) 
    File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/ 
    Versions/2.7/lib/python2.7/logging/config.py", line 155, in _install_handlers 
    args = eval(args, vars(logging)) 
    File "<string>", line 1 
    ('alogger.log', mode='a', maxBytes=25000, 
    backupCount=0, encoding=None, delay=0) 
            ^
    SyntaxError: invalid syntax 

回答

1

在你的配置,('alogger.log')是不是一個有效參數元組,而事實上整段看起來是錯誤的。 RotatingFileHandler有以下參數:

filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0 

並且您需要指定反映此的參數元組。您尚未指定maxBytes值,因此不會發生翻轉;而1440看起來像是保留的奇數個備份日誌文件。查看文檔以確保您使用處理程序的__init__.py的正確參數。

更新:省去參數的名字,像這樣:

args=('alogger.log', 'a', 25000, 0, None, 0) 
+0

感謝。不過,我現在得到了一個不同的錯誤。我改變了這部分閱讀在上面的問題編輯部分中提到的跟蹤它下面的回溯。 – user2480526