自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

【博文推薦 】 python Howto之logging模塊

開發(fā) 后端
我們不要通過logging.Logger來直接實(shí)例化得到logger,而是需要通過logging.getLogger("name")來生成logger對象。
 本博文出自51CTO博客無名博主,有任何問題請進(jìn)入博主頁面互動(dòng)討論!
博文地址:http://xdzw608.blog.51cto.com/4812210/1608718

本文來源于對py2.7.9 docs中howto-logging部分加之源代碼的理解。官方文檔鏈接如下,我用的是下載的pdf版本,應(yīng)該是一致的:https://docs.python.org/2/howto/logging.html

我們不按照文檔上由淺入深的講解順序,因?yàn)榫瓦@么點(diǎn)東西不至于有“入”這個(gè)動(dòng)作。

使用logging模塊記錄日志涉及四個(gè)主要類,使用官方文檔中的概括最為合適:

logger提供了應(yīng)用程序可以直接使用的接口;

handler將(logger創(chuàng)建的)日志記錄發(fā)送到合適的目的輸出;

filter提供了細(xì)度設(shè)備來決定輸出哪條日志記錄;

formatter決定日志記錄的最終輸出格式。

寫log的一般順序?yàn)椋?/strong>

一、創(chuàng)建logger:

我們不要通過logging.Logger來直接實(shí)例化得到logger,而是需要通過logging.getLogger("name")來生成logger對象。

不 是說我們不能實(shí)現(xiàn)Logger的實(shí)例化,而是我們期待的是同一個(gè)name得到的是同一個(gè)logger,這樣多模塊之間可以共同使用同一個(gè) logger,getLogger正是這樣的解決方案,它內(nèi)部使用loggerDict字典來維護(hù),可以保證相同的名字作為key會得到同一個(gè) logger對象。我們可以通過實(shí)例來驗(yàn)證一下:

  1. #test_logger1.py 
  2.  
  3. #coding:utf-8 
  4.  
  5. import logging 
  6.  
  7. print logging.getLogger("mydear"
  8.  
  9. import test_logger2 
  10.  
  11. test_logger2.run()   #調(diào)用文件2中的函數(shù),保證兩個(gè)模塊共同處于生存期 
  12.  
  13. #test_logger2.py 
  14.  
  15. #coding:utf-8 
  16.   
  17. import logging 
  18.  
  19. def run(): 
  20.  
  21. print logging.getLogger("mydear"

輸出:

<logging.Logger object at 0x00000000020ECF28>
<logging.Logger object at 0x00000000020ECF28>

結(jié)果表明兩個(gè)文件中通過"mydear"調(diào)用getLogger可以保證得到的logger對象是同一個(gè)。而分別進(jìn)行Logger類的實(shí)例化則不能保證。

有了logger之后就可以配置這個(gè)logger,例如設(shè)置日志級別setLevel,綁定控制器addHandler,添加過濾器addFilter等。

配置完成后,就可以調(diào)用logger的方法寫日志了,根據(jù)5個(gè)日志級別對應(yīng)有5個(gè)日志記錄方法,分別為logger.debug,logger.info,logger.warning,logger.error,logger.critical。 

二、配置Logger對象的日志級別:

logger.setLevel(logging.DEBUG)  #DEBUG以上的日志級別會被此logger處理 

三、創(chuàng)建handler對象

handler 負(fù)責(zé)將log分發(fā)到某個(gè)目的輸出,存在多種內(nèi)置的Handler將log分發(fā)到不同的目的地,或是控制臺,或是文件,或是某種形式的stream,或是 socket等。一個(gè)logger可以綁定多個(gè)handler,例如,一條日志可以同時(shí)輸出到控制臺和文件中。

以FileHandler和StreamHandler為例:

logfile= logging.FileHandler("./log.txt")  #創(chuàng)建一個(gè)handler,用于將日志輸出到文件中   

console = logging.StreamHandler() #創(chuàng)建另一個(gè)handler,將日志導(dǎo)向流

    handler對象也需要設(shè)置日志級別,由于一個(gè)logger可以包含多個(gè)handler,所以每個(gè)handler設(shè)置日志級別是有必要的。用通俗的話 講,比如,我們需要處理debug以上級別的消息,所以我們將logger的日志級別定為DEBUG;然后我們想把error以上的日志輸出到控制臺,而 DEBUG以上的消息輸出到文件中,這種分流就需要兩個(gè)Handler來控制。

logfile.setLevel(logging.DEBUG)

console.setLevel(logging.ERROR)

除了對handler對象設(shè)置日志級別外,還可以指定formatter,即日志的輸出格式。對handler對象設(shè)置日志格式,說明了可以將一條記錄以不同的格式輸出到控制臺,文件或其他目的地。

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logfile.setFormatter(formatter) #設(shè)置handler的日志輸出格式

formatter創(chuàng)建時(shí)使用的關(guān)鍵字,***會以列表的形式展現(xiàn),這不是重點(diǎn)。

四、綁定handler到logger中

至此handlers和logger已經(jīng)準(zhǔn)備好了,下面我們就將handlers綁定到logger上,一個(gè)logger對象可以綁定多個(gè)handler。

logger.addHandler(logfile)  #logger是通過getLogger得到的Logger對象

logger.addHandler(console) 

五、使用logger真正寫日志

logger.debug("some debug message.")

logger.info("some info message.")

看上去,中間步驟(創(chuàng)建handler,設(shè)置日志級別,設(shè)置輸出格式等)更像是配置Logger,一旦配置完成則直接調(diào)用寫日志的接口即可,稍后這些日志將按照先前的配置輸出。

嗚呼,好多內(nèi)容啊,來點(diǎn)簡單的吧.

下面的代碼,是最簡單的。導(dǎo)入logging之后就進(jìn)行了寫日志操作:

  1. #coding:utf-8 
  2. import logging 
  3.  
  4. logging.debug("debug mes"
  5.  
  6. logging.info("info mes"
  7.  
  8. logging.warning("warn mes"

控制臺輸出如下:

WARNING:root:warn mes

咦?發(fā)生了什么情況,為什么只輸出了warning?handler、logger、formatter去哪兒了?

-_-!說好的最簡單的呢?為了讓自己講信用,我盡可能把它解釋成“最簡單的”。

#p#

知識點(diǎn)1:logger間存在繼承關(guān)系

logger 通過名字來決定繼承關(guān)系,如果一個(gè)logger的名字是"mydest",另一個(gè)logger的名字是"mydest.dest1" (getLogger("mydest.dest1")),那么就稱后者是前者的子logger,會繼承前者的配置。上面的代碼沒有指定logger,直接調(diào)用logging.debug等方法時(shí),會使用所有l(wèi)ogger的祖先類RootLogger。

從上面的代碼運(yùn)行結(jié)果可以猜測出,該RootLogger設(shè)置的日志級別是logging.WARN,輸出目的地是標(biāo)準(zhǔn)流。從源碼可以更清楚的看出來:

  1. root = RootLogger(WARNING)  #設(shè)置WARNING的級別 

至于rootLogger的輸出目的地的配置,我們跟蹤logging.debug的源代碼來看一下:

  1. def debug(msg, *args, **kwargs): 
  2.  
  3. ""
  4.  
  5. Log a message with severity 'DEBUG' on the root logger. 
  6.  
  7. ""
  8.  
  9. if len(root.handlers) == 0
  10.  
  11. basicConfig() 
  12.  
  13. root.debug(msg, *args, **kwargs) 

大約可以看到,如果rootLogger沒有配置handler,就會不帶參數(shù)運(yùn)行basicConfig函數(shù)(*請看知識點(diǎn)2),我們看一下basicConfig的源代碼:

  1. def basicConfig(**kwargs): 
  2.  
  3. _acquireLock() 
  4.  
  5. try
  6.  
  7. if len(root.handlers) == 0
  8.  
  9. filename = kwargs.get("filename"
  10.  
  11. if filename: 
  12.  
  13. mode = kwargs.get("filemode"'a'
  14.  
  15. hdlr = FileHandler(filename, mode) 
  16.  
  17. else
  18.  
  19. stream = kwargs.get("stream"
  20.  
  21. hdlr = StreamHandler(stream) 
  22.  
  23. fs = kwargs.get("format", BASIC_FORMAT) 
  24.  
  25. dfs = kwargs.get("datefmt"None
  26.  
  27. fmt = Formatter(fs, dfs) 
  28.  
  29. hdlr.setFormatter(fmt) 
  30.  
  31. root.addHandler(hdlr) 
  32.  
  33. level = kwargs.get("level"
  34.  
  35. if level is not None
  36.  
  37. root.setLevel(level) 
  38.  
  39. finally
  40.  
  41. _releaseLock() 

因 為參數(shù)為空,所以我們就看出了,該rootLoger使用了不帶參數(shù)的StreamHandler,也可以看到諸如format之類的默認(rèn)配置。之后我們 跟蹤StreamHandler(因?yàn)槲覀兿肟吹饺罩据敵瞿康牡氐呐渲?,而handler就是控制日志流向的,所以我們要跟蹤它)的源代碼:

 

  1. class StreamHandler(Handler): 
  2.  
  3. ""
  4.  
  5. A handler class which writes logging records, appropriately formatted, 
  6.  
  7. to a stream. Note that this class does not close the stream, as 
  8.  
  9. sys.stdout or sys.stderr may be used. 
  10.  
  11. ""
  12.  
  13. def __init__(self, stream=None): 
  14.  
  15. ""
  16.  
  17. Initialize the handler. 
  18.  
  19. If stream is not specified, sys.stderr is used. 
  20.  
  21. ""
  22.  
  23. Handler.__init__(self) 
  24.  
  25. if stream is None: 
  26.  
  27. stream = sys.stderr  #### 
  28.  
  29. self.stream = stream 

不帶參數(shù)的StreamHandler將會把日志流定位到sys.stderr流,標(biāo)準(zhǔn)錯(cuò)誤流同樣會輸出到控制臺

知識點(diǎn)2:basicConfig函數(shù)用來配置RootLogger

    basicConfig函數(shù)僅用來配置RootLogger,rootLogger是所有Logger的祖先Logger,所以其他一切Logger會繼承該Logger的配置。

從上面的basicConfig源碼看,它可以有六個(gè)關(guān)鍵字參數(shù),分別為:

filename:執(zhí)行使用該文件名為rootLogger創(chuàng)建FileHandler,而不是StreamHandler

filemode:指定文件打開方式,默認(rèn)是"a"

stream:指定一個(gè)流來初始化StreamHandler。此參數(shù)不能和filename共存,如果同時(shí)提供了這兩個(gè)參數(shù),則stream參數(shù)被忽略

format:為rootLogger的handler指定輸出格式

datefmt:指定輸出的日期時(shí)間格式

level:設(shè)置rootLogger的日志級別

使用樣例:

 

  1. logging.basicConfig( 
  2.  
  3.    filename = './log.txt'
  4.  
  5.    filemode = 'a'
  6.  
  7.    #stream = sys.stdout, 
  8.  
  9.    format = '%(levelname)s:%(message)s'
  10.  
  11.    datefmt = '%m/%d/%Y %I:%M:%S'
  12.  
  13.    level = logging.DEBUG 

知識點(diǎn)3 通過示例詳細(xì)討論Logger配置的繼承關(guān)系

首先準(zhǔn)備下繼承條件:log2繼承自log1,logger的名稱可以隨意,要注意‘.’表示的繼承關(guān)系。

 

  1. #coding:utf-8 
  2. import logging 
  3.  
  4. log1 = logging.getLogger("mydear"
  5.  
  6. log1.setLevel(logging.WARNING) 
  7.  
  8. log1.addHandler(StreamHandler()) 
  9.  
  10. log2 = logging.getLogger("mydear.app"
  11.  
  12. log2.error("display"
  13.  
  14. log2.info("not display"

    level的繼承

原則:子logger寫日志時(shí),優(yōu)先使用本身設(shè)置了的level;如果沒有設(shè)置,則逐層向上級父logger查詢,直到查詢到為止。最極端的情況是,使用rootLogger的默認(rèn)日志級別logging.WARNING。

從源代碼中看更為清晰, 感謝python的所見即所得:

  1. def getEffectiveLevel(self): 
  2.  
  3. """ 
  4.  
  5. Get the effective level for this logger. 
  6.  
  7. Loop through this logger and its parents in the logger hierarchy, 
  8.  
  9. looking for a non-zero logging level. Return the first one found. 
  10.  
  11. """ 
  12.  
  13. logger = self 
  14.  
  15. while logger: 
  16.  
  17. if logger.level: 
  18.  
  19. return logger.level 
  20.  
  21. logger = logger.parent 
  22.  
  23. return NOTSET 

handler的繼承

原則:先將日志對象傳遞給子logger的所有handler處理,處理完畢后,如果該子logger的propagate屬性沒有設(shè)置為0,則將日志對象向上傳遞給***個(gè)父Logger,該父logger的所有handler處理完畢后,如果它的propagate也沒有設(shè)置為0,則繼續(xù)向上層傳遞,以此類推。最終的狀態(tài),要么遇到一個(gè)Logger,它的propagate屬性設(shè)置為了0;要么一直傳遞直到rootLogger處理完畢。

在上面實(shí)例代碼的基礎(chǔ)上,我們再添加一句代碼,即:

  1. #coding:utf-8 
  2.  
  3. import logging 
  4.  
  5. log1 = logging.getLogger("mydear"
  6.  
  7. log1.setLevel(logging.WARNING) 
  8.  
  9. log1.addHandler(StreamHandler()) 
  10.  
  11. log2 = logging.getLogger("mydear.app"
  12.  
  13. log2.error("display"
  14.  
  15. log2.info("not display"
  16.  
  17. print log2.handlers  #打印log2綁定的handler 

輸出如下:

display
[]

說好的繼承,但是子logger竟然沒有綁定父類的handler,what's wrong?

看到下面調(diào)用handler的源代碼,就真相大白了??梢岳斫獬桑@不是真正的(類)繼承,只是"行為上的繼承":

  1. def callHandlers(self, record): 
  2.  
  3. ""
  4.  
  5. Pass a record to all relevant handlers. 
  6. Loop through all handlers for this logger and its parents in the 
  7.  
  8. logger hierarchy. If no handler was found, output a one-off error 
  9.  
  10. message to sys.stderr. Stop searching up the hierarchy whenever a 
  11.  
  12. logger with the "propagate" attribute set to zero is found - that 
  13.  
  14. will be the last logger whose handlers are called. 
  15.  
  16. ""
  17.  
  18. c = self 
  19.  
  20. found = 0 
  21.  
  22. while c: 
  23.  
  24. for hdlr in c.handlers: #首先遍歷子logger的所有handler 
  25.  
  26. found = found + 1 
  27.  
  28. if record.levelno >= hdlr.level: 
  29.  
  30. hdlr.handle(record) 
  31.  
  32. if not c.propagate: #如果logger的propagate屬性設(shè)置為0,停止 
  33.  
  34. c = None#break out  
  35.  
  36. else:   #否則使用直接父logger 
  37.  
  38. c = c.parent 
  39.  
  40. ... 

額,最簡單的樣例牽引出來這么多后臺的邏輯,不過我們懂一下也是有好處的。

下面,我們將一些零碎的不是很重要的東西羅列一下,這篇就結(jié)束了。

1.幾種LogLevel是全局變量,以整數(shù)形式表示,也可以但是不推薦自定義日志級別,如果需要將level設(shè)置為用戶配置,則獲取level和檢查level的一般代碼是:

  1. #假設(shè)loglevel代表用戶設(shè)置的level內(nèi)容 
  2.  
  3. numeric_level = getattr(logging, loglevel.upper(), None) 
  4.  
  5. if not isinstance(numeric_level, int): 
  6.  
  7. raise ValueError('Invalid log level: %s' % loglevel) 
  8.  
  9. logging.basicConfig(level=numeric_level, ...) 

2.format格式,用于創(chuàng)建formatter對象,或者basicConfig中,就不翻譯了

%(name)s    Name of the logger (logging channel)
    %(levelno)s Numeric logging level for the message (DEBUG, INFO,
    WARNING, ERROR, CRITICAL)
    %(levelname)s   Text logging level for the message ("DEBUG", "INFO",
    "WARNING", "ERROR", "CRITICAL")
    %(pathname)s    Full pathname of the source file where the logging
    call was issued (if available)
    %(filename)s    Filename portion of pathname
    %(module)s  Module (name portion of filename)
    %(lineno)d  Source line number where the logging call was issued
    (if available)
    %(funcName)s    Function name
    %(created)f Time when the LogRecord was created (time.time()
    return value)
    %(asctime)s Textual time when the LogRecord was created
    %(msecs)d   Millisecond portion of the creation time
    %(relativeCreated)d Time in milliseconds when the LogRecord was created,
    relative to the time the logging module was loaded
    (typically at application startup time)
    %(thread)d  Thread ID (if available)
    %(threadName)s  Thread name (if available)
    %(process)d Process ID (if available)
    %(message)s The result of record.getMessage(), computed just as
    the record is emitted

3.寫日志接口

logging.warn("%s am a hero", "I")   #1 %格式以參數(shù)形式提供實(shí)參

    logging.warn("%s am a hero" % ("I",)) #2 直接提供字符串,也可以使用format,template

    logging.warn("%(name)s am a hero", {'name':"I"})  #關(guān)鍵字參數(shù)   

logging.warn("%(name)s am a hero" % {'name':"I"}) #甚至這樣也可以

logging.warn("%(name)s am a hero, %(value)s" % {'name':"I", 'value':'Yes'}) #原來%也能解析關(guān)鍵字參數(shù),不一定非是元組

如果關(guān)鍵字和位置參數(shù)混用呢,%應(yīng)該不會有什么作為了,***也就能這樣:

logging.warn("%(name)s am a hero, %()s" % {'name':"I" ,'': 'Yes'})#也是字典格式化的原理

4.配置logging:

上 面已經(jīng)講了如果配置handler,綁定到logger。如果需要一個(gè)稍微龐大的日志系統(tǒng),可以想象,我們會使用好多的 addHandler,SetFormatter之類的,有夠煩了。幸好,logging模塊提供了兩種額外配置方法,不需要寫眾多代碼,直接從配置結(jié)構(gòu) 中獲悉我們的配置意圖

方式一:使用配置文件

 

  1. import logging 
  2.  
  3. import logging.config 
  4.  
  5. logging.config.fileConfig('logging.conf'
  6.  
  7. # create logger 
  8.  
  9. logger = logging.getLogger('simpleExample'
  10.  
  11. # 'application' code 
  12.  
  13. logger.debug('debug message'
  14.  
  15. logger.info('info message'
  16.  
  17. logger.warn('warn message'
  18.  
  19. logger.error('error message'
  20.  
  21. logger.critical('critical message'
  22.  
  23.   
  24.  
  25. #配置文件logging.conf的內(nèi)容 
  26.  
  27. [loggers] 
  28.  
  29. keys=root,simpleExample 
  30.  
  31. [handlers] 
  32.  
  33. keys=consoleHandler 
  34.  
  35. [formatters] 
  36.  
  37. keys=simpleFormatter 
  38.  
  39. [logger_root] 
  40.  
  41. level=DEBUG 
  42.  
  43. handlers=consoleHandler 
  44.  
  45. [logger_simpleExample] 
  46.  
  47. level=DEBUG 
  48.  
  49. handlers=consoleHandler 
  50.  
  51. qualname=simpleExample 
  52.  
  53. propagate=0 
  54.  
  55. [handler_consoleHandler] 
  56.  
  57. class=StreamHandler 
  58.  
  59. level=DEBUG 
  60.  
  61. formatter=simpleFormatter 
  62.  
  63. args=(sys.stdout,) 
  64.  
  65. [formatter_simpleFormatter] 
  66.  
  67. format=%(asctime)s - %(name)s - %(levelname)s - %(message)s 
  68.  
  69. datefmt= 

方式二:使用字典

請參閱python2.7.9 Library文檔,鏈接:

https://docs.python.org/2/library/logging.config.html?highlight=dictconfig#configuration-dictionary-schema

5.眾多的handler滿足不同的輸出需要

StreamHandler,F(xiàn)ileHandler,NullHandler,RotatingFileHandler,TimedRotatingFileHandler,SocketHandler,DatagramHandler,SMTPHandler,SysLogHandler,NTEventLogHandler,MemoryHandler,HTTPHandler,WatchedFileHandler,

其中前三種在logging模塊中給出,其他的在logging.handlers模塊中給出。

 

 

 

責(zé)任編輯:王雪燕 來源: 51CTO博客
相關(guān)推薦

2015-05-15 10:04:28

localhost

2015-03-09 14:53:04

OracleOracle DGDataGuard F

2014-12-01 10:33:51

Python

2011-03-15 13:19:27

iptablesHOWTO

2015-06-17 09:34:09

軟件定義存儲 云存儲

2015-07-01 10:25:07

Docker開源項(xiàng)目容器

2014-12-12 10:46:55

Azure地緣組affinitygro

2015-06-15 13:06:23

項(xiàng)目項(xiàng)目經(jīng)驗(yàn)

2022-03-04 09:59:16

logging模塊函數(shù)程序

2014-10-23 09:47:28

安全運(yùn)維Iperf

2015-07-03 11:26:07

MySQL高可用架MHA

2015-04-21 09:28:58

ockerdocker監(jiān)控平臺監(jiān)控

2015-07-29 13:46:27

OpenStackIcehouse私有云實(shí)戰(zhàn)部署

2015-04-17 11:17:15

大數(shù)據(jù)大數(shù)據(jù)擇業(yè)

2014-12-23 11:23:14

DRBDHeartbeatNFS

2015-03-02 09:22:09

Javascript函數(shù)用法apply

2015-04-13 11:34:56

Windows DocNano ServerPaaS

2015-06-17 09:43:45

云計(jì)算應(yīng)用傳統(tǒng)企業(yè)應(yīng)用云平臺

2014-11-25 11:33:35

2015-12-10 10:13:22

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號