欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中使用logging模塊打印log日志詳解

 更新時(shí)間:2015年04月05日 10:46:34   投稿:junjie  
這篇文章主要介紹了Python中使用logging模塊打印log日志詳解,本文講解了logging模塊介紹、基本使用方法、高級(jí)使用方法、使用實(shí)例等,需要的朋友可以參考下

學(xué)一門(mén)新技術(shù)或者新語(yǔ)言,我們都要首先學(xué)會(huì)如何去適應(yīng)這們新技術(shù),其中在適應(yīng)過(guò)程中,我們必須得學(xué)習(xí)如何調(diào)試程序并打出相應(yīng)的log信息來(lái),正所謂“只要log打的好,沒(méi)有bug解不了”,在我們熟知的一些信息技術(shù)中,log4xxx系列以及開(kāi)發(fā)Android app時(shí)的android.util.Log包等等都是為了開(kāi)發(fā)者更好的得到log信息服務(wù)的。在Python這門(mén)語(yǔ)言中,我們同樣可以根據(jù)自己的程序需要打出log。

log信息不同于使用打樁法打印一定的標(biāo)記信息,log可以根據(jù)程序需要而分出不同的log級(jí)別,比如info、debug、warn等等級(jí)別的信息,只要實(shí)時(shí)控制log級(jí)別開(kāi)關(guān)就可以為開(kāi)發(fā)人員提供更好的log信息,與log4xx類(lèi)似,logger,handler和日志消息的調(diào)用可以有具體的日志級(jí)別(Level),只有在日志消息的級(jí)別大于logger和handler的設(shè)定的級(jí)別,才會(huì)顯示。下面我就來(lái)談?wù)勎以赑ython中使用的logging模塊一些方法。

logging模塊介紹

Python的logging模塊提供了通用的日志系統(tǒng),熟練使用logging模塊可以方便開(kāi)發(fā)者開(kāi)發(fā)第三方模塊或者是自己的Python應(yīng)用。同樣這個(gè)模塊提供不同的日志級(jí)別,并可以采用不同的方式記錄日志,比如文件,HTTP、GET/POST,SMTP,Socket等,甚至可以自己實(shí)現(xiàn)具體的日志記錄方式。下文我將主要介紹如何使用文件方式記錄log。

logging模塊包括logger,handler,filter,formatter這四個(gè)基本概念。

logger:提供日志接口,供應(yīng)用代碼使用。logger最長(zhǎng)用的操作有兩類(lèi):配置和發(fā)送日志消息??梢酝ㄟ^(guò)logging.getLogger(name)獲取logger對(duì)象,如果不指定name則返回root對(duì)象,多次使用相同的name調(diào)用getLogger方法返回同一個(gè)logger對(duì)象。
handler:將日志記錄(log record)發(fā)送到合適的目的地(destination),比如文件,socket等。一個(gè)logger對(duì)象可以通過(guò)addHandler方法添加0到多個(gè)handler,每個(gè)handler又可以定義不同日志級(jí)別,以實(shí)現(xiàn)日志分級(jí)過(guò)濾顯示。
filter:提供一種優(yōu)雅的方式?jīng)Q定一個(gè)日志記錄是否發(fā)送到handler。
formatter:指定日志記錄輸出的具體格式。formatter的構(gòu)造方法需要兩個(gè)參數(shù):消息的格式字符串和日期字符串,這兩個(gè)參數(shù)都是可選的。

基本使用方法

一些小型的程序我們不需要構(gòu)造太復(fù)雜的log系統(tǒng),可以直接使用logging模塊的basicConfig函數(shù)即可,代碼如下:

復(fù)制代碼 代碼如下:

'''
Created on 2012-8-12
 
@author: walfred
@module: loggingmodule.BasicLogger
'''
import logging
 
log_file = "./basic_logger.log"
 
logging.basicConfig(filename = log_file, level = logging.DEBUG)
 
logging.debug("this is a debugmsg!")
logging.info("this is a infomsg!")
logging.warn("this is a warn msg!")
logging.error("this is a error msg!")
logging.critical("this is a critical msg!")

運(yùn)行程序時(shí)我們就會(huì)在該文件的當(dāng)前目錄下發(fā)現(xiàn)basic_logger.log文件,查看basic_logger.log內(nèi)容如下:

復(fù)制代碼 代碼如下:

INFO:root:this is a info msg!
DEBUG:root:this is a debug msg!
WARNING:root:this is a warn msg!
ERROR:root:this is a error msg!
CRITICAL:root:this is a critical msg!

需要說(shuō)明的是我將level設(shè)定為DEBUG級(jí)別,所以log日志中只顯示了包含該級(jí)別及該級(jí)別以上的log信息。信息級(jí)別依次是:notset、debug、info、warn、error、critical。如果在多個(gè)模塊中使用這個(gè)配置的話(huà),只需在主模塊中配置即可,其他模塊會(huì)有相同的使用效果。

較高級(jí)版本

上述的基礎(chǔ)使用比較簡(jiǎn)單,沒(méi)有顯示出logging模塊的厲害,適合小程序用,現(xiàn)在我介紹一個(gè)較高級(jí)版本的代碼,我們需要依次設(shè)置logger、handler、formatter等配置。

復(fù)制代碼 代碼如下:

'''
Created on 2012-8-12
 
@author: walfred
@module: loggingmodule.NomalLogger
'''
import logging
 
log_file = "./nomal_logger.log"
log_level = logging.DEBUG
 
logger = logging.getLogger("loggingmodule.NomalLogger")
handler = logging.FileHandler(log_file)
formatter = logging.Formatter("[%(levelname)s][%(funcName)s][%(asctime)s]%(message)s")
 
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(log_level)
 
#test
logger.debug("this is a debug msg!")
logger.info("this is a info msg!")
logger.warn("this is a warn msg!")
logger.error("this is a error msg!")
logger.critical("this is a critical msg!")

這時(shí)我們查看當(dāng)前目錄的nomal_logger.log日志文件,如下:

復(fù)制代碼 代碼如下:

[DEBUG][][2012-08-12 17:43:59,295]this is a debug msg!
[INFO][][2012-08-12 17:43:59,295]this is a info msg!
[WARNING][][2012-08-12 17:43:59,295]this is a warn msg!
[ERROR][][2012-08-12 17:43:59,295]this is a error msg!
[CRITICAL][][2012-08-12 17:43:59,295]this is a critical msg!

這個(gè)對(duì)照前面介紹的logging模塊,不難理解,下面的最終版本將會(huì)更加完整。

完善版本

這個(gè)最終版本我用singleton設(shè)計(jì)模式來(lái)寫(xiě)一個(gè)Logger類(lèi),代碼如下:

復(fù)制代碼 代碼如下:

'''
Created on 2012-8-12
 
@author: walfred
@module: loggingmodule.FinalLogger
'''
 
import logging.handlers
 
class FinalLogger:
 
 logger = None
 
 levels = {"n" : logging.NOTSET,
  "d" : logging.DEBUG,
  "i" : logging.INFO,
  "w" : logging.WARN,
  "e" : logging.ERROR,
  "c" : logging.CRITICAL}
 
 log_level = "d"
 log_file = "final_logger.log"
 log_max_byte = 10 * 1024 * 1024;
 log_backup_count = 5
 
 @staticmethod
 def getLogger():
  if FinalLogger.logger is not None:
   return FinalLogger.logger
 
  FinalLogger.logger = logging.Logger("oggingmodule.FinalLogger")
  log_handler = logging.handlers.RotatingFileHandler(filename = FinalLogger.log_file,\
  maxBytes = FinalLogger.log_max_byte,\
  backupCount = FinalLogger.log_backup_count)
  log_fmt = logging.Formatter("[%(levelname)s][%(funcName)s][%(asctime)s]%(message)s")
  log_handler.setFormatter(log_fmt)
  FinalLogger.logger.addHandler(log_handler)
  FinalLogger.logger.setLevel(FinalLogger.levels.get(FinalLogger.log_level))
  return FinalLogger.logger
 
if __name__ == "__main__":
 logger = FinalLogger.getLogger()
 logger.debug("this is a debug msg!")
 logger.info("this is a info msg!")
 logger.warn("this is a warn msg!")
 logger.error("this is a error msg!")
 logger.critical("this is a critical msg!")

當(dāng)前目錄下的 final_logger.log內(nèi)容如下:

復(fù)制代碼 代碼如下:

[DEBUG][][2012-08-12 18:12:23,029]this is a debug msg!
[INFO][][2012-08-12 18:12:23,029]this is a info msg!
[WARNING][][2012-08-12 18:12:23,029]this is a warn msg!
[ERROR][][2012-08-12 18:12:23,029]this is a error msg!
[CRITICAL][][2012-08-12 18:12:23,029]this is a critical msg!

這個(gè)final版本,也是我一直用的,讀者朋友也可以再加上其他的一些Handler,比如StreamHandler等等來(lái)獲取更多的log信息,當(dāng)然也可以將你的log信息通過(guò)配置文件來(lái)完成。

相關(guān)文章

最新評(píng)論