日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Pytest使用logging模塊寫日志的實(shí)例詳解_python

作者:redrose2100 ? 更新時(shí)間: 2023-01-01 編程語言

logging是python語言中的一個(gè)日志模塊,專門用來寫日志的,日志級(jí)別通常分為debug、info、warning、error、critical幾個(gè)級(jí)別,一般情況下,默認(rèn)的日志級(jí)別為warning,在調(diào)試或者測試階段,日志級(jí)別可以設(shè)置為debug或者info,當(dāng)在生產(chǎn)環(huán)境上線后日志級(jí)別一般為warning或者error級(jí)別,下面就快速體驗(yàn)一下logging模塊寫日志的用法,這里創(chuàng)建一個(gè)python文件,比如demo.py 文件,然后在即可在python文件中使用logging寫日志了,比如如下代碼,使用logging對(duì)每一個(gè)日志級(jí)別分別寫了一條日志。

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文件,結(jié)果如下,可以看出,此時(shí)僅打印了warning、error、critical級(jí)別的日志,這是因?yàn)閜ython中默認(rèn)的級(jí)別是warning級(jí)別。所以低于warning級(jí)別的日志都不會(huì)打印了。

(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>

當(dāng)然在代碼中是可以修改日志級(jí)別的,比如如下代碼即將日志級(jí)別修改為了debug級(jí)別。

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文件,可以看出此時(shí)已經(jīng)將debug和info級(jí)別的日志都顯示出來了。

(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往日志文件寫日志也是很容易的,如下代碼配置一個(gè)文件即可,同時(shí)可以設(shè)置日志的級(jí)別,比如這里仍然設(shè)置為debug級(jí)別,即debug以及以上級(jí)別的日志均會(huì)寫入日志文件。

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")

此時(shí)再次執(zhí)行demo.py文件,可以發(fā)現(xiàn)此時(shí)控制臺(tái)并沒有打印,而是在當(dāng)前目錄下生成了一個(gè)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

默認(rèn)情況下寫日志文件日志是按照追加的模式,比如再次執(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

當(dāng)然是可以設(shè)置寫入日志的模式,比如如下filemode模式設(shè)置為w,則表示每次清空文件再寫日志,當(dāng)然如果把filemode設(shè)置為a則為追加模式,如果不設(shè)置,默認(rèn)情況下也是追加模式。

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")

此時(shí)再次執(zhí)行demo.py文件,此時(shí)因?yàn)槟J皆O(shè)置為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í),都是會(huì)顯示文件、時(shí)間、代碼行數(shù)等信息,這里也是可以設(shè)置的。比如如下,這里直接設(shè)置日志格式,并直接在控制臺(tái)打印。

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í)行結(jié)果如下,可以看到此時(shí)日志中有時(shí)間戳、日志級(jí)別、代碼文件、代碼函數(shù),日志內(nèi)容等。這個(gè)格式基本就是我們希望要的日志格式。

(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>

當(dāng)然python中的logging模塊還有許多其他高級(jí)的應(yīng)用,在pytest中只需要這么簡單的用logging即可,因此這里就不再深入的介紹logging了。

原文鏈接:https://blog.csdn.net/redrose2100/article/details/128179049

欄目分類
最近更新