Python:记录TypeError:并非在字符串格式化期间转换了所有参数 [英] Python: Logging TypeError: not all arguments converted during string formatting

查看:293
本文介绍了Python:记录TypeError:并非在字符串格式化期间转换了所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在做什么

>>> import logging
>>> logging.getLogger().setLevel(logging.INFO)
>>> from datetime import date
>>> date = date.today()
>>> logging.info('date={}', date)
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 846, in emit
    msg = self.format(record)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 723, in format
    return fmt.format(record)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 464, in format
    record.message = record.getMessage()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file <stdin>, line 1
>>> 

我的python版本是

My python version is

$ python --version
Python 2.7.3

我如何使其工作?

推荐答案

您可以自己进行格式化:

You could do the formatting yourself:

logging.info('date={}'.format(date))

正如Martijn Pieters指出的那样,这将始终运行字符串格式,而使用日志记录模块将导致仅在实际记录了消息的情况下执行格式化.

As was pointed out by Martijn Pieters, this will always run the string formatting, while using the logging module would cause the formatting to only be performed if the message is actually logged.

这篇关于Python:记录TypeError:并非在字符串格式化期间转换了所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆