国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Python基礎(chǔ)之logging模塊知識總結(jié)

瀏覽:4日期:2022-06-19 13:05:53
目錄前言一、日志級別二、basicConfig三、日志寫文件四、traceback記錄前言

logging模塊是Python內(nèi)置的標(biāo)準(zhǔn)模塊,主要用于輸出腳本運(yùn)行日志,可以設(shè)置輸出日志的等級、日志保存路徑等。

可以通過設(shè)置不同的日志等級,在 release 版本中只輸出重要信息,而不顯示大量的調(diào)試信息 logging 可以決定將信息輸出位置和內(nèi)容 logging 線程更安全一、日志級別

級別排序:CRITICAL > ERROR > WARNING > INFO > DEBUG

debug : 打印全部日志,詳細(xì)信息,通常只出現(xiàn)在診斷問題 info : 打印info,warning,error,critical級別的日志,正常輸出 warning : 打印warning,error,critical級別的日志,部分異常,不影響程序 error : 打印error,critical級別的日志,影響程序部分功能 critical : 打印critical級別,影響程序運(yùn)行

import logging # 引入logging模塊# 將信息打印到控制臺上logging.debug('debug')logging.info('info')logging.warning('warning')logging.error('error')logging.critical('critical')[root@zijie ~]# python log.pyWARNING:root:warningERROR:root:errorCRITICAL:root:critical

默認(rèn)生成的root logger的level是logging.WARNING,低于該級別不輸出,如果要展示低于WARNING級別的內(nèi)容,可以引入logging.basicConfig指定日志級別logging.basicConfig(level=logging.DEBUG)

二、basicConfig格式 描述 filename 指定使用指定的文件名而不是 StreamHandler 創(chuàng)建 FileHandler。 filemode 如果指定 filename,則以此模式打開文件(‘r’、‘w’、‘a(chǎn)’)。默認(rèn)為“a”。 format 為處理程序使用指定的格式字符串。 datefmt 使用 time.strftime() 所接受的指定日期/時(shí)間格式。 style 如果指定了格式,則對格式字符串使用此樣式?!?’ 用于 printf 樣式、’{’ 用于 str.format()、’$’ 用于 string。默認(rèn)為“%”。 level 將根記錄器級別設(shè)置為指定的級別。默認(rèn)生成的 root logger 的 level 是 logging.WARNING,低于該級別的就不輸出了。級別排序:CRITICAL > ERROR > WARNING > INFO > DEBUG。(如果需要顯示所有級別的內(nèi)容,可將 level=logging.NOTSET) stream 使用指定的流初始化 StreamHandler。注意,此參數(shù)與 filename 不兼容——如果兩者都存在,則會拋出 ValueError。 handlers 如果指定,這應(yīng)該是已經(jīng)創(chuàng)建的處理程序的迭代,以便添加到根日志程序中。任何沒有格式化程序集的處理程序都將被分配給在此函數(shù)中創(chuàng)建的默認(rèn)格式化程序。注意,此參數(shù)與 filename 或 stream 不兼容——如果兩者都存在,則會拋出 ValueError。

import logginglogging.basicConfig(level=logging.INFO, format=’%(asctime)s %(filename)s %(levelname)s %(message)s’, datefmt=’%a %d %b %Y %H:%M:%S’, filename=’xuehui.log’, filemode=’w’)logging.info(’This is a info.’)logging.debug(’This is a debug message.’)logging.warning(’This is a warning.’)三、日志寫文件

import loggingimport os.pathimport time#創(chuàng)建loggerlogger = logging.getLogger()logger.setLevel(logging.DEBUG)# 創(chuàng)建handler,用于寫入日志文件logdate = time.strftime(’%Y%m%d%H%M%S’, time.localtime(time.time()))log_path = ’logs/’log_name = log_path + logdate + ’.log’logfile = log_namefh = logging.FileHandler(logfile, mode=’w’)fh.setLevel(logging.DEBUG)# 定義輸出格式formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')fh.setFormatter(formatter)# 將logger添加到handlerlogger.addHandler(fh)# 日志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’)四、traceback記錄

import loggingimport os.pathimport time#創(chuàng)建loggerlogger = logging.getLogger()logger.setLevel(logging.DEBUG)# 創(chuàng)建handler,用于寫入日志文件logdate = time.strftime(’%Y%m%d%H%M%S’, time.localtime(time.time()))log_path = ’logs/’log_name = log_path + logdate + ’.log’logfile = log_namefh = logging.FileHandler(logfile, mode=’w’)fh.setLevel(logging.DEBUG)# 定義輸出格式formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')fh.setFormatter(formatter)# 將logger添加到handlerlogger.addHandler(fh)# 日志try: open(’/data/exist’, ’rb’)except BaseException as e: logger.error(’Failed to open file’, exc_info=True)

到此這篇關(guān)于Python基礎(chǔ)之logging模塊知識總結(jié)的文章就介紹到這了,更多相關(guān)Python logging模塊內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 金沙县| 乌审旗| 荆门市| 苍南县| 新龙县| 呼玛县| 南昌县| 凌源市| 安平县| 五原县| 曲周县| 巴马| 新干县| 安庆市| 延边| 宽城| 吕梁市| 东山县| 正阳县| 新巴尔虎左旗| 留坝县| 鄯善县| 平原县| 霍邱县| 吕梁市| 阜康市| 扎赉特旗| 承德市| 荥阳市| 观塘区| 固安县| 阿拉尔市| 密山市| 新邵县| 扶绥县| 文昌市| 枣庄市| 舟曲县| 巴中市| 同江市| 钦州市|