【问题标题】:Python logging: control carriage return while output in the INFO modePython日志记录:在INFO模式下输出时控制回车
【发布时间】:2018-11-09 05:29:48
【问题描述】:

当我必须记录这样的消息时:

logging.info('An operation will be performed...')
some code here
logging.info('DONE')

我希望DONE 与上一条消息在同一行。是否可以在日志包中实现这种行为?

此外,我在当前项目中看到有时会在同一行记录输出消息,有时则不会。我很难理解是什么决定了这些操作。

【问题讨论】:

标签: python logging


【解决方案1】:

虽然您可以通过创建自己的处理程序来做到这一点(请参阅this question 的答案),但这在可读性方面并不是一个好主意。

你可以得到相同的结果,而无需做任何 hacky;例如:

log_statement = 'An operation will be performed... %s'

try:
    if True:
        result = 'TRUE'
    else:
        result = 'UNREACHABLE'
except Exception as e:
    result = str(e)

logging.info(log_statement, result)

此外,我在当前项目中看到有时会在同一行记录输出消息,有时则不会。我很难理解是什么决定了这些操作。

您看到的行为似乎与您想象的不完全一样,但没有代码示例,我们无法判断。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2011-11-02
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多