在Python編程中,日誌是很重要的,不僅可以幫助開發者調試程序,還能夠在應用程序出現故障時快速定位問題。Python內置了日誌模塊,可以用來方便地記錄和輸出日誌信息。本文從多個方面介紹Python如何列印日誌。
一、Python列印日誌信息
1、Python中的日誌函數有:debug(), info(), warning(), error(), critical()。除了debug()以外,info()、warning()、error()、critical()這四種日誌函數都有相應的level,分別對應日誌記錄的級別。
import logging logging.warning("這是一個警告") logging.error("這是一個錯誤") logging.info("這是一條信息") logging.debug("這是一條調試信息") logging.critical("這是一個嚴重的錯誤")
2、Python的日誌記錄器(logger)允許按層次結構來設定不同的日誌級別,從而實現對各個模塊的日誌處理。
import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) logger.debug("This is a debug message") logger.info("This is an information") logger.warning("This is a warning") logger.error("This is an error") logger.critical("This is a critical")
二、Python列印網頁
在Python中,可以使用requests庫來獲取網頁內容,並使用logging模塊列印出來。
import requests import logging url = "http://www.example.com" res = requests.get(url) logging.debug(res.text)
三、Python列印日誌函數
在Python中,使用logging模塊的basicConfig函數或者dictConfig函數來設定日誌格式,代碼示例如下:
import logging logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') logging.debug('This message will be logged')
四、Python列印日曆
使用Python中的calendar模塊,可以實現列印日曆,代碼示例如下:
import logging import calendar c = calendar.TextCalendar(calendar.MONDAY) str = c.formatmonth(2018,10) logging.info(str)
五、Python列印日誌到屏幕
Python的logging模塊默認把日誌輸出到了標準錯誤,可以通過basicConfig函數的stream參數來改變輸出的位置,代碼示例如下:
import logging logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s') logging.debug('This message will be printed on stdout')
六、Python列印日誌沒有行號
有時候在列印日誌的時候,輸出中會出現行號,可以使用Formatter類去掉行號:
import logging log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' formatter = logging.Formatter(log_format) handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger(__name__) logger.addHandler(handler) logger.setLevel(logging.DEBUG) logger.debug('Debugging...')
七、Python列印圖紙
使用Python的turtle模塊畫圖,然後使用logging模塊列印圖形:
import turtle import logging logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') t = turtle.Turtle() for i in range(4): t.fd(100) t.lt(90) logging.info("Drawing a square using turtle module")
八、Python列印log日誌
使用Python的logging模塊可以方便地列印log日誌:
import logging logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') 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')
九、Python列印圖形
使用Python的matplotlib模塊繪製圖形,並將圖形信息列印到日誌文件中:
import matplotlib.pyplot as plt import numpy as np import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') x = np.arange(0,2*np.pi,0.1) y = np.sin(x) plt.plot(x, y) plt.show() logging.info("Drawing a sine wave using matplotlib module")
本文介紹了Python列印日誌的多個方面,包括日誌函數、日誌記錄器、列印網頁、列印日曆、列印日誌到屏幕、去掉行號、列印圖紙、列印log日誌以及列印圖形等。使用Python的logging模塊,可以方便地記錄和輸出日誌信息,幫助開發者輕鬆調試程序。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/294060.html