共計 613 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,可以使用內置的 logging 模塊來打印日志文件。以下是一個簡單的示例代碼:
import logging
# 設置日志級別
logging.basicConfig(filename='example.log', level=logging.DEBUG)
# 打印日志信息
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
在上面的示例中,我們通過調用 logging.basicConfig()
來設置日志文件名稱和日志級別。然后使用 logging.debug()
,logging.info()
,logging.warning()
,logging.error()
和logging.critical()
方法來打印不同級別的日志信息到文件中。
運行該代碼后,將在當前目錄下生成一個名為 example.log
的日志文件,并在文件中記錄相應級別的日志信息。
丸趣 TV 網 – 提供最優質的資源集合!
正文完