Pytest使用logging模塊寫日志的實例詳解
logging是python語言中的一個日志模塊,專門用來寫日志的,日志級別通常分為debug、info、warning、error、critical幾個級別,一般情況下,默認的日志級別為warning,在調(diào)試或者測試階段,日志級別可以設置為debug或者info,當在生產(chǎn)環(huán)境上線后日志級別一般為warning或者error級別,下面就快速體驗一下logging模塊寫日志的用法,這里創(chuàng)建一個python文件,比如demo.py 文件,然后在即可在python文件中使用logging寫日志了,比如如下代碼,使用logging對每一個日志級別分別寫了一條日志。
import logging logging.debug("this is debug log") logging.info("this is info log") logging.warning("this is warning log") logging.error("this is error log") logging.critical("this is critical log")
執(zhí)行python文件,結果如下,可以看出,此時僅打印了warning、error、critical級別的日志,這是因為python中默認的級別是warning級別。所以低于warning級別的日志都不會打印了。
(demo-HCIhX0Hq) E:\demo>python demo.py WARNING:root:this is warning log ERROR:root:this is error log CRITICAL:root:this is critical log (demo-HCIhX0Hq) E:\demo>
當然在代碼中是可以修改日志級別的,比如如下代碼即將日志級別修改為了debug級別。
import logging logging.basicConfig(level=logging.DEBUG) logging.debug("this is debug log") logging.info("this is info log") logging.warning("this is warning log") logging.error("this is error log") logging.critical("this is critical log")
再次執(zhí)行demo.py文件,可以看出此時已經(jīng)將debug和info級別的日志都顯示出來了。
(demo-HCIhX0Hq) E:\demo>python demo.py DEBUG:root:this is debug log INFO:root:this is info log WARNING:root:this is warning log ERROR:root:this is error log CRITICAL:root:this is critical log (demo-HCIhX0Hq) E:\demo>
利用logging往日志文件寫日志也是很容易的,如下代碼配置一個文件即可,同時可以設置日志的級別,比如這里仍然設置為debug級別,即debug以及以上級別的日志均會寫入日志文件。
import logging logging.basicConfig(filename="demo.log",level=logging.DEBUG) logging.debug("this is debug log") logging.info("this is info log") logging.warning("this is warning log") logging.error("this is error log") logging.critical("this is critical log")
此時再次執(zhí)行demo.py文件,可以發(fā)現(xiàn)此時控制臺并沒有打印,而是在當前目錄下生成了一個demo.log文件,內(nèi)容如下:
DEBUG:root:this is debug log INFO:root:this is info log WARNING:root:this is warning log ERROR:root:this is error log CRITICAL:root:this is critical log
默認情況下寫日志文件日志是按照追加的模式,比如再次執(zhí)行一次,則demo.log中的內(nèi)容即變?yōu)槿缦聝?nèi)容:
DEBUG:root:this is debug log INFO:root:this is info log WARNING:root:this is warning log ERROR:root:this is error log CRITICAL:root:this is critical log DEBUG:root:this is debug log INFO:root:this is info log WARNING:root:this is warning log ERROR:root:this is error log CRITICAL:root:this is critical log
當然是可以設置寫入日志的模式,比如如下filemode模式設置為w,則表示每次清空文件再寫日志,當然如果把filemode設置為a則為追加模式,如果不設置,默認情況下也是追加模式。
import logging logging.basicConfig(filename="demo.log",filemode='w',level=logging.DEBUG) logging.debug("this is debug log") logging.info("this is info log") logging.warning("this is warning log") logging.error("this is error log") logging.critical("this is critical log")
此時再次執(zhí)行demo.py文件,此時因為模式設置為w了,因此demo.log內(nèi)容先清空再寫入,即內(nèi)容如下:
DEBUG:root:this is debug log INFO:root:this is info log WARNING:root:this is warning log ERROR:root:this is error log CRITICAL:root:this is critical log
平常我們在查看其他產(chǎn)品的日志時,都是會顯示文件、時間、代碼行數(shù)等信息,這里也是可以設置的。比如如下,這里直接設置日志格式,并直接在控制臺打印。
import logging logging.basicConfig(format=("%(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s"), datefmt="%Y-%m-%d_%H:%M:%S", level=logging.DEBUG) logging.debug("this is debug log") logging.info("this is info log") logging.warning("this is warning log") logging.error("this is error log") logging.critical("this is critical log")
執(zhí)行結果如下,可以看到此時日志中有時間戳、日志級別、代碼文件、代碼函數(shù),日志內(nèi)容等。這個格式基本就是我們希望要的日志格式。
(demo-HCIhX0Hq) E:\demo>python demo.py 2022-12-04_22:47:14 | DEBUG | demo.py:6 | this is debug log 2022-12-04_22:47:14 | INFO | demo.py:7 | this is info log 2022-12-04_22:47:14 | WARNING | demo.py:8 | this is warning log 2022-12-04_22:47:14 | ERROR | demo.py:9 | this is error log 2022-12-04_22:47:14 | CRITICAL | demo.py:10 | this is critical log (demo-HCIhX0Hq) E:\demo>
當然python中的logging模塊還有許多其他高級的應用,在pytest中只需要這么簡單的用logging即可,因此這里就不再深入的介紹logging了。
到此這篇關于Pytest使用logging模塊寫日志的實例詳解的文章就介紹到這了,更多相關Pytest logging模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談算法之最小生成樹Kruskal的Python實現(xiàn)
最小生成樹Kruskal算法可以稱為“加邊法”,初始最小生成樹邊數(shù)為0,每迭代一次就選擇一條滿足條件的最小代價邊,加入到最小生成樹的邊集合里。本文將介紹它的原理,并用Python進行實現(xiàn)2021-06-06python接口自動化之ConfigParser配置文件的使用詳解
這篇文章主要介紹了python接口自動化之ConfigParser配置文件的使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Django網(wǎng)絡框架之HelloDjango項目創(chuàng)建教程
這篇文章主要介紹了Django網(wǎng)絡框架之HelloDjango項目創(chuàng)建,結合實例形式詳細分析了Django框架創(chuàng)建HelloDjango項目的具體步驟與詳細實現(xiàn)技巧,需要的朋友可以參考下2019-06-06Pytorch在dataloader類中設置shuffle的隨機數(shù)種子方式
今天小編就為大家分享一篇Pytorch在dataloader類中設置shuffle的隨機數(shù)種子方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python3+OpenCV2實現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉、仿射)
這篇文章主要介紹了Python3+OpenCV2實現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉、仿射),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05使用python實現(xiàn)一個簡單ping?pong服務器
這篇文章主要為大家介紹了使用python實現(xiàn)一個簡單ping?pong服務器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04