我一直在尝试停止使用打印语句进行调试并开始使用logging.debug。我可以在打印中使用运算符,但日志记录似乎没有做同样的事情。
例如,这有效
print('Is between now and then: ', solar_noon < now < solarnoonplustimeunit )
这这不是。
logger.debug('Is between now and then: ', solar_noon < now < solarnoonplustimeunit )
后者说:
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 471, in format
record.message = record.getMessage()
File "/usr/lib/python2.7/logging/__init__.py", line 335, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
我应该如何使用日志记录进行运算符(operator)测试?
最佳答案
日志记录模块方法期望第一个参数是格式字符串,并且以下参数属于该格式字符串:logger.debug(fmt_str, arg1,....)
。但是您的字符串没有任何迹象表明后面有更多元素。
即logger.debug 函数最终会尝试做这样的事情:
fmt_str % (arg1, arg2, ...)
尝试仅添加格式 str %s
:
logger.debug('Is between now and then: %s', solar_noon < now < solarnoonplustimeunit )
编辑:为什么原始字符串适用于print
:
根据docs :
print(*objects, sep=' ', end='\n', file=sys.stdout)
Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
由于默认的 sep
是一个空格,Python 只是将字符串和 bool 值的字符串表示形式混合在一起,并用空格分隔它们。
关于python - 如何使用运算符(operator)进行logging.debug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123161/