在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-hk/n/294060.html
微信掃一掃
支付寶掃一掃