简体   繁体   中英

Python: Logger failing to log object

Here is the logger configuration

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)

Here is how I am using it

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

Where Transaction is an entity and __repr__ looks like

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)

My server log says

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

What is I am not doing right here?

It should be %(message)s rather than %(messages)s .

See the Basic example in the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM