溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

logging模塊如何在Python中使用

發(fā)布時(shí)間:2021-03-17 16:54:56 來源:億速云 閱讀:183 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)logging模塊如何在Python中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

logging模塊

函數(shù)式簡(jiǎn)單配置

import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

logging.basicConfig()函數(shù)中可通過具體參數(shù)來更改logging模塊默認(rèn)行為,可用參數(shù)有:

  • filename:用指定的文件名創(chuàng)建FiledHandler,這樣日志會(huì)被存儲(chǔ)在指定的文件中。

  • filemode:文件打開方式,在指定了filename時(shí)使用這個(gè)參數(shù),默認(rèn)值為“a”還可指定為“w”。

  • format:指定handler使用的日志顯示格式。

  • datefmt:指定日期時(shí)間格式。

  • level:設(shè)置rootlogger(后邊會(huì)講解具體概念)的日志級(jí)別

  • stream:用指定的stream創(chuàng)建StreamHandler??梢灾付ㄝ敵龅?code>sys.stderr,sys.stdout或者文件(f=open('test.log','w')),默認(rèn)為sys.stderr。若同時(shí)列出了filename和stream兩個(gè)參數(shù),則stream參數(shù)會(huì)被忽略。

format參數(shù)中可能用到的格式化串:

%(name)sLogger的名字
%(levelno)s數(shù)字形式的日志級(jí)別
%(levelname)s文本形式的日志級(jí)別
%(pathname)s調(diào)用日志輸出函數(shù)的模塊的完整路徑名,可能沒有
%(filename)s調(diào)用日志輸出函數(shù)的模塊的文件名
%(module)s調(diào)用日志輸出函數(shù)的模塊名
%(funcName)s調(diào)用日志輸出函數(shù)的函數(shù)名
%(lineno)d調(diào)用日志輸出函數(shù)的語句所在的代碼行
%(created)f當(dāng)前時(shí)間,用UNIX標(biāo)準(zhǔn)的表示時(shí)間的浮 點(diǎn)數(shù)表示
%(relativeCreated)d輸出日志信息時(shí)的,自Logger創(chuàng)建以 來的毫秒數(shù)
%(asctime)s字符串形式的當(dāng)前時(shí)間。默認(rèn)格式是 “2003-07-08 16:49:45,896”。逗號(hào)后面的是毫秒
%(thread)d線程ID。可能沒有
%(threadName)s線程名??赡軟]有
%(process)d進(jìn)程ID。可能沒有
%(message)s用戶輸出的消息

logging庫(kù)提供了多個(gè)組件:Logger、Handler、Filter、Formatter。Logger對(duì)象提供應(yīng)用程序可直接使用的接口,Handler發(fā)送日志到適當(dāng)?shù)哪康牡?,F(xiàn)ilter提供了過濾日志信息的方法,F(xiàn)ormatter指定日志顯示格式。另外,可以通過:logger.setLevel(logging.Debug)設(shè)置級(jí)別,當(dāng)然,也可以通過fh.setLevel(logging.Debug)單對(duì)文件流設(shè)置某個(gè)級(jí)別。

def my_logger(filename,leval,file = True,stream = True):
  logger = logging.getLogger()
  formatter = logging.Formatter (fmt = '%(asctime)s--%(message)s----%(name)s--%(levelname)s--%(lineno)d',
                  datefmt = '%d/%m/%y %H:%M:%S')
  logger.setLevel(leval)
  if file:
    file_handler = logging.FileHandler(filename, encoding='utf-8')
    logger.addHandler(file_handler)
    file_handler.setFormatter(formatter)
  if stream:
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
  return logger
logger = my_logger('logging',logging.DEBUG)
logger.warning('warn!!!')

運(yùn)行結(jié)果:

28/08/18 09:48:53--warn!!!----root--WARNING--27

上述就是小編為大家分享的logging模塊如何在Python中使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI