0

我想我错过了一些重要的东西,对于我的生活,我无法弄清楚。我有一个logging.conf文件,我正在尝试xyz.py读取我的主要(比如 )文件。但我收到了这个奇怪的错误。我有下面的回溯,然后是配置文件 -logging.conf然后是xyz.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(我按照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),'localhost@localhost.com', ['abc@bca.com'],
                        '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 is 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
4

1 回答 1

3

在您的配置中,('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)
于 2013-10-22T23:07:52.977 回答