python - 如何使用运算符(operator)进行logging.debug?

标签 python logging printing operator-keyword

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

相关文章:

wpf - WPF打印-在WPF PrintDialog上自动设置打印机

打印时选择标记中的 CSS 隐藏下拉箭头

c# - 什么是通过互操作/pinvoke 提供从 c++ dll 到 c# 应用程序的日志记录的方法

java - windows下tomcat中的system.out.println写到哪里了?

python - 从[-1,1]创建网格,以将共形贴图应用于图像

javascript - webapp2 cookie 检索不完整

android - 为什么Android Studio 的logcat 没有完整输出日志?

c - 在 C 中帮助 While 循环

python - 在 Python 中读取文本文件的重复数字

python - 使用 PyTorch 进行二元分类的目标和输出形状/类型