Python常用模塊logging——日志輸出功能(示例代碼)
用途
logging模塊是Python的內(nèi)置模塊,主要用于輸出運(yùn)行日志,可以靈活配置輸出日志的各項(xiàng)信息。
基本使用方法
logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a")
參數(shù)
日志一共分成5個(gè)等級(jí),從低到高分別是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。
%(levelno)s: 打印日志級(jí)別的數(shù)值
%(levelname)s: 打印日志級(jí)別名稱
%(pathname)s: 打印當(dāng)前執(zhí)行程序的路徑,其實(shí)就是sys.argv[0]
%(filename)s: 打印當(dāng)前執(zhí)行程序名
%(funcName)s: 打印日志的當(dāng)前函數(shù)
%(lineno)d: 打印日志的當(dāng)前行號(hào)
%(asctime)s: 打印日志的時(shí)間
%(thread)d: 打印線程ID
%(threadName)s: 打印線程名稱
%(process)d: 打印進(jìn)程ID
%(message)s: 打印日志信息
調(diào)用
logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message')
示例
import logging logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a") logging.debug("Hello")
日志文件loggmsg.log
levelname:DEBUG filename: test.py outputNumber: [7] thread: MainThread output msg: Hello -
總結(jié)
以上所述是小編給大家介紹的Python常用模塊logging——日志輸出功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
python寫入csv時(shí)writerow()和writerows()函數(shù)簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于python寫入csv時(shí)writerow()和writerows()函數(shù)的相關(guān)資料,writerows和writerow是Python中csv模塊中的兩個(gè)函數(shù),用于將數(shù)據(jù)寫入CSV文件,需要的朋友可以參考下2023-07-07Python?Httpx庫實(shí)現(xiàn)超跑式網(wǎng)絡(luò)請(qǐng)求用法實(shí)例
這篇文章主要為大家介紹了Python?Httpx庫實(shí)現(xiàn)超跑式網(wǎng)絡(luò)請(qǐng)求用法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01python使用urllib2實(shí)現(xiàn)發(fā)送帶cookie的請(qǐng)求
這篇文章主要介紹了python使用urllib2實(shí)現(xiàn)發(fā)送帶cookie的請(qǐng)求,涉及Python操作cookie的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Python?OpenCV的基本使用及相關(guān)函數(shù)
這篇文章主要介紹了Python-OpenCV的基本使用和相關(guān)函數(shù)介紹,主要包括圖像的讀取保存圖像展示問題,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05python在windows下創(chuàng)建隱藏窗口子進(jìn)程的方法
這篇文章主要介紹了python在windows下創(chuàng)建隱藏窗口子進(jìn)程的方法,涉及Python使用subprocess模塊操作進(jìn)程的相關(guān)技巧,需要的朋友可以參考下2015-06-06