python3 日志检索异常raise KeyError(key),KeyError: 'formatters'

Trse KeyError(key)aceback (most recent call last):
  File "/root/pyrun/wechat_crawl/articles_update_exec.py", line 3, in
    from articles_update import *
  File "/root/pyrun/wechat_crawl/articles_update.py", line 11, in
    logging.config.fileConfig('resources/logging.conf')
  File "/usr/local/python3/lib/python3.6/logging/config.py", line 76, in fileConfig
    formatters = _create_formatters(cp)
  File "/usr/local/python3/lib/python3.6/logging/config.py", line 109, in _create_formatters
    flist = cp["formatters"]["keys"]
  File "/usr/local/python3/lib/python3.6/configparser.py", line 959, in __getitem__
    raise KeyError(key)
KeyError: 'formatters'

原因分析,python3启动时,检索的不是项目包下的目录文件,所以日志文件必须指明路径。我原来的日志方式是:

logging.config.fileConfig('resources/wechat_read_add.conf')
logger = logging.getLogger()

修改为:

from os import path
log_file_path = path.join(path.dirname(path.abspath(__file__)), 'resources/logging.conf')
logging.config.fileConfig(log_file_path)
#logging.config.fileConfig('resources/logging.conf')
logger = logging.getLogger()

你可能感兴趣的:(人工智能)