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

