Python ---记录错误--- AttributeError但找不到位置?

时间:2020-06-22 10:07:24

标签: python logging

你好,我用2个处理程序和1个格式化程序在python中编写了一个简单的代码,得到了AttributeError,但找不到位置。 这是我的代码:

import logging
from logging.handlers import RotatingFileHandler


#Configuration loggeur
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler(filename='activity2.log', mode='a', maxBytes=1000000, backupCount= 2, encoding='utf-8')

file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.DEBUG)
logger.addHandler(stream_handler)

logger.warning('On y va')

这里是输出:

--- Logging error ---
Traceback (most recent call last):
  File "D:\Anaconda\envs\scapenv\lib\logging\__init__.py", line 1081, in emit
    msg = self.format(record)
  File "D:\Anaconda\envs\scapenv\lib\logging\__init__.py", line 925, in format
    return fmt.format(record)
AttributeError: 'int' object has no attribute 'format'
Call stack:
  File "d:/02 - Programmes/08 - Passerelles/HBS/St Julien - GS Nelson Mandela/Py Excel/testlog.py", line 32, in <module>
    logger.warning('On y va')
Message: 'On y va'
Arguments: ()

如果您有一些线索...

1 个答案:

答案 0 :(得分:2)

从底部开始的第三行:

stream_handler.setFormatter(logging.DEBUG)

您使用logging.DEBUG代替formatter作为参数。

修复:

stream_handler.setFormatter(formatter)