的Python:记录仪无法记录对象

问题描述:

这里是记录器配置的Python:记录仪无法记录对象

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

+0

为我的失明道歉,谢谢你的快速响应 – daydreamer

+0

也解决了我的问题。非常感谢! – shawn