Python常用模塊logging——日志輸出功能(示例代碼)
用途
logging模塊是Python的內置模塊,主要用于輸出運行日志,可以靈活配置輸出日志的各項信息。
基本使用方法
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")
參數
日志一共分成5個等級,從低到高分別是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。
%(levelno)s: 打印日志級別的數值
%(levelname)s: 打印日志級別名稱
%(pathname)s: 打印當前執(zhí)行程序的路徑,其實就是sys.argv[0]
%(filename)s: 打印當前執(zhí)行程序名
%(funcName)s: 打印日志的當前函數
%(lineno)d: 打印日志的當前行號
%(asctime)s: 打印日志的時間
%(thread)d: 打印線程ID
%(threadName)s: 打印線程名稱
%(process)d: 打印進程ID
%(message)s: 打印日志信息
調用
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 -
總結
以上所述是小編給大家介紹的Python常用模塊logging——日志輸出功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
相關文章
python寫入csv時writerow()和writerows()函數簡單示例
這篇文章主要給大家介紹了關于python寫入csv時writerow()和writerows()函數的相關資料,writerows和writerow是Python中csv模塊中的兩個函數,用于將數據寫入CSV文件,需要的朋友可以參考下2023-07-07
python使用urllib2實現發(fā)送帶cookie的請求
這篇文章主要介紹了python使用urllib2實現發(fā)送帶cookie的請求,涉及Python操作cookie的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
python在windows下創(chuàng)建隱藏窗口子進程的方法
這篇文章主要介紹了python在windows下創(chuàng)建隱藏窗口子進程的方法,涉及Python使用subprocess模塊操作進程的相關技巧,需要的朋友可以參考下2015-06-06

