导入日志记录配置时的Python字典ValueError

时间:2013-06-10 09:38:54

标签: python logging python-2.7

我通过谷歌进行了搜索,发现只有关于Django和它的网址的答案。 我的问题类似,但涉及日志问题,我无法应对。

我使用了Advanced Logging TutorialLogging Cookbook以及logging.conf description

并在运行“python logger.py”之后总是得到以下错误:

Traceback (most recent call last):
  File "logger.py", line 5, in <module>
    logging.config.dictConfig('logger-dict.conf')
  File "/usr/lib/python2.7/logging/config.py", line 777, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 380, in __init__
    self.config = ConvertingDict(config)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

logger.py

import logging
import logging.config

logging.config.dictConfig('logger-dict.conf')

logger = logging.getLogger('basic')

logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

记录器-dict.conf

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'simple': {
        'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        'datefmt': '%d/%m/%Y %H:%M:%S %Z',
    },
},
'handlers': {
    'console':{
        'level':'DEBUG',
        'class':'logging.StreamHandler',
        'formatter': 'simple'
    },
    'file':{
        'level': 'WARNING',
        'class': 'logging.handlers.RotatingFileHandler',
        'formatter': 'simple',
        'mode': 'a',
        'filename': './logs/logconfig.log',
        'maxBytes': '1048576',
        'backupCount': '5',
    },

},
'loggers': {
    'basic': {
        'handlers':['file'],
        'propagate': True,
        'level':'INFO',
    },
}
}

我一直试图用关于Django的答案来解决这个问题,但现在还没有成功。

修改

在下面的评论之后,我明白如果把字典放在某个地方,最好将它放入* .py文件而不是* .conf。

但是有一种简单的方法(不是逐行读取文件)来使用带有python字典的* .conf文件吗?

0 个答案:

没有答案
相关问题