gpt4 book ai didi

Python:记录器无法记录对象

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:00 26 4
gpt4 key购买 nike

这是记录器配置

APPLICATION_NAME = 'myapp'

# -- setting up global logger - #
logger = logging.getLogger(APPLICATION_NAME)
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler(APPLICATION_NAME + '.log')

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(messages)s')
fh.setFormatter(formatter)

logger.addHandler(fh)

我是这样用的

logger.debug('added transaction: %s', str(transaction))

其中 Transaction 是一个实体,__repr__ 看起来像

def __repr__(self):
return '<Transaction:%s:%s:%s:%s:%s:%s:%s>' % (
self.uuid, self.name, self.created_on, self.amount,
'debit' if self.debit else 'credit', self.user, self.category)

我的服务器日志说

Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 842, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 719, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 467, in format
s = self._fmt % record.__dict__
KeyError: 'messages'
Logged from file transaction_manager.py, line 30

我在这里没有做什么?

最佳答案

它应该是 %(message)s 而不是 %(messages)s

参见 Basic example在文档中。

关于Python:记录器无法记录对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com