【问题标题】:How to show logging message in Python如何在 Python 中显示日志消息
【发布时间】:2019-07-08 16:54:43
【问题描述】:

我需要在调试时显示日志消息,我应该如何使用如下代码显示消息:

logs.out("Here are the error messages")
logs.clear()

【问题讨论】:

标签: error-logging


【解决方案1】:

您应该使用日志库, 这是一个例子:

import logging

logging.debug('debug message')
logging.info('information message')
logging.warning('A warning message')
logging.error('Error Description')
logging.critical('Critical messages should be here')

【讨论】:

  • 试试这样的:logging.basicConfig(filename='file.log', filemode='w', format='%(name)s - %(levelname)s - %(message) s')
【解决方案2】:

如果您希望只输出一些常用文本进行日志记录,那么您可以选择 Python 的 print() 函数,或者您希望以正确的方式进行日志记录,那么您可以使用 logging API由 Python 本身作为库提供。

`

import logging
logging.basicConfig(filename='example.log', level=logging.debug)
logging.debug("Debugging")

`

【讨论】:

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