網(wǎng)站首頁 編程語言 正文
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
相關(guān)推薦
- 2022-05-02 Python?私有屬性與私有方法_python
- 2023-04-02 pytorch?transform數(shù)據(jù)處理轉(zhuǎn)c++問題_python
- 2023-12-14 如何統(tǒng)計(jì)一個(gè)字符在字符串中出現(xiàn)次數(shù)
- 2022-10-16 Python?讀取?Word?文檔操作_python
- 2022-04-22 Go 字符串時(shí)間等相互轉(zhuǎn)化以及時(shí)間加減等操作
- 2023-07-05 讓Linux中的SCP遠(yuǎn)程復(fù)制不再需要輸入密碼
- 2024-01-10 Maven導(dǎo)入org.apache.commons.lang3.StringUtils
- 2022-10-19 Docker鏡像與容器的導(dǎo)入導(dǎo)出以及常用命令總結(jié)_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支