Python中使用logging和traceback模塊記錄日志和跟蹤異常
logging模塊
logging模塊用于輸出運(yùn)行日志,可以設(shè)置不同的日志等級,保存信息到日志文件中等。 相比print,logging可以設(shè)置日志的等級,控制在發(fā)布版本中的輸出內(nèi)容,并且可以指定日志的輸出格式。
1. 使用logging在終端輸出日志
#!/usr/bin/env python # -*- coding:utf-8 -*- import logging # 引入logging模塊 # 設(shè)置打印日志級別 CRITICAL > ERROR > WARNING > INFO > DEBUG logging.basicConfig(level = logging.DEBUG,format = '%(asctime)s - %(name)s -%(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s') # 將信息打印到控制臺上 logging.debug(u"調(diào)試") logging.info(u"執(zhí)行打印功能") logging.warning(u"警告") logging.error(u"錯誤") logging.critical(u"致命錯誤")
輸出

2. 使用logging在終端輸出日志,並保存日志到本地log文件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging # 引入logging模塊
import os.path
# 第一步,創(chuàng)建一個logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # Log等級開關(guān)
# 第二步,創(chuàng)建一個handler,用于寫入日志文件
log_path = os.path.dirname(os.getcwd()) + '/Logs/'
log_name = log_path + 'log.log'
logfile = log_name
file_handler = logging.FileHandler(logfile, mode='a+')
file_handler.setLevel(logging.ERROR) # 輸出到file的log等級的開關(guān)
# 第三步,定義handler的輸出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
file_handler.setFormatter(formatter)
# 第四步,將handler添加到logger里面
logger.addHandler(file_handler)
# 如果需要同時需要在終端上輸出,定義一個streamHandler
print_handler = logging.StreamHandler() # 往屏幕上輸出
print_handler.setFormatter(formatter) # 設(shè)置屏幕上顯示的格式
logger.addHandler(print_handler)
# 日志信息
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')
# 或使用logging
logging.debug('this is a logger debug message')
logging.info('this is a logger info message')
logging.warning('this is a logger warning message')
logging.error('this is a logger error message')
logging.critical('this is a logger critical message')
日志等級劃分
- FATAL:致命錯誤
- CRITICAL:特別糟糕的事情,如內(nèi)存耗盡、磁盤空間為空,一般很少使用
- ERROR:發(fā)生錯誤時,如IO操作失敗或者連接問題
- WARNING:發(fā)生很重要的事件,但是并不是錯誤時,如用戶登錄密碼錯誤
- INFO:處理請求或者狀態(tài)變化等日常事務(wù)
- DEBUG:調(diào)試過程中使用DEBUG等級,如算法中每個循環(huán)的中間狀態(tài)
traceback模塊
traceback是python中用來跟蹤異常信息的模塊,方便把程序中的運(yùn)行異常打印或者保存下來做異常分析。
常見用法
try: doSomething() except: traceback.print_exc() # logging.error(str(traceback.format_exc()))
traceback.format_exc() 與 traceback.print_exc() 區(qū)別:
- traceback.format_exc() 返回異常信息的字符串,可以用來把信息記錄到log里;
- traceback.print_exc() 直接把異常信息在終端打印出來;
traceback.print_exc()也可以實現(xiàn)把異常信息寫入文件,使用方法:
traceback.print_exc(file=open('traceback_INFO.txt','w+'))
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Python traceback模塊獲取異常信息的使用
- 解決python問題 Traceback (most recent call last)
- python如何利用traceback獲取詳細(xì)的異常信息
- Python基于traceback模塊獲取異常信息
- 淺談python出錯時traceback的解讀
- Python 輸出詳細(xì)的異常信息(traceback)方式
- python3 使用traceback定位異常實例
- 基于python traceback實現(xiàn)異常的獲取與處理
- Python異常模塊traceback用法實例分析
- 搞清楚 Python traceback的具體使用方法
- 淺談Python traceback的優(yōu)雅處理
- python traceback捕獲并打印異常的方法
- Python?Traceback(most?recent?call?last)報錯信息:示例解讀
相關(guān)文章
pycharm不以pytest方式運(yùn)行,想要切換回普通模式運(yùn)行的操作
這篇文章主要介紹了pycharm不以pytest方式運(yùn)行,想要切換回普通模式運(yùn)行的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
使用python+poco+夜神模擬器進(jìn)行自動化測試實例
這篇文章主要介紹了使用python+poco+夜神模擬器進(jìn)行自動化測試實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

