如何使用运算符进行logging.debug?

问题描述 投票:1回答:1

我一直在尝试停止使用print语句进行调试,并开始使用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

如何使用日志记录进行操作员测试?

python logging printing operator-keyword
1个回答
1
投票

日志记录模块方法期望第一个参数是格式字符串,并且以下args属于该格式字符串:logger.debug(fmt_str, arg1,....)。但是你的字符串没有任何迹象表明会有更多元素出现。

即logger.debug func最终将尝试执行以下操作:

fmt_str % (arg1, arg2, ...)

尝试添加格式str %s

logger.debug('Is between now and then:    %s', solar_noon < now < solarnoonplustimeunit )

Edit: Why your original string works with print:

根据docs

print(* objects,sep ='',end ='\ n',file = sys.stdout)

将对象打印到流文件,由sep分隔,然后结束。 sep,end和file(如果存在)必须作为关键字参数给出。

所有非关键字参数都转换为字符串,如str(),并写入流,由sep分隔,后跟end。 sep和end都必须是字符串;它们也可以是None,这意味着使用默认值。如果没有给出对象,print()将只写入结束。

由于默认的sep是一个空格,python只是将字符串的字符串表示和bool值混合在一起,用空格分隔它们。

© www.soinside.com 2019 - 2024. All rights reserved.