歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Python學習之logging模塊

Python自帶日志處理模塊logging
默認的日志級別有DEBUG,INFO,WARNING,ERROR,CRITICAL,對應的函數是debug(),info(),warning(),error()和critical()

In [490]: import logging
 
In [491]: LOG_FILENAME='/tmp/example.log'
 
In [492]: logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
 
In [493]: logging.debug('This message should go to the log file')

 

查看/tmp/example.log的內容
$ cat /tmp/example.log 
DEBUG:root:This message should go to the log file

不斷執行

logging.debug('This message should go to the log file')

/tmp/example.log會不斷地刷新


日志輪轉
In [639]: import glob
 
In [640]: import logging
 
In [641]: import logging.handlers
 
In [642]: LOG_FILENAME='/tmp/logging_rotatingfile_example.out'
 
In [643]: my_logger=logging.getLogger('MyLogger')
 
In [644]: my_logger.setLevel(logging.DEBUG)
 
In [645]: handler=logging.handlers.RotatingFileHandler(LOG_FILENAME,maxBytes=20,backupCount=5)
 
In [646]: my_logger.addHandler(handler)
 
In [648]: for i in range(20):
    my_logger.debug('i=%d' % i)
  .....:     
  .....:     
 
In [650]: logfiles=glob.glob('%s*' %LOG_FILENAME)
 
In [651]: for filename in logfiles:
  .....:    print filename
  .....:     
  .....:     
/tmp/logging_rotatingfile_example.out.1
/tmp/logging_rotatingfile_example.out.2
/tmp/logging_rotatingfile_example.out.5
/tmp/logging_rotatingfile_example.out.3
/tmp/logging_rotatingfile_example.out.4
/tmp/logging_rotatingfile_example.out

 

最近的日志文件總是名為logging_rotatingfile_example.out,一旦大小達到maxBytes設置的20字節,就將輪轉。


logging模塊最重要的一個功能就是可以根據設置的不同的日志級別輸出不同的日志信息。
import logging
import sys
 
LEVELS={'debug':logging.DEBUG,
        'info' :logging.INFO,
        'warning':logging.WARNING,
        'error':logging.ERROR,
        'critical':logging.CRITICAL
        }
 
if sys.argv > 1:
  level_name = sys.argv[1]
  level=LEVELS.get(level_name,logging.NOTSET)
  logging.basicConfig(level=level)
 
logging.debug('This is a debug message')
logging.info('This is a info message')
logging.warning('This is a warning message')
logging.error('This is a error message')
logging.critical('This is a critical error message')
~                                                                                                                       
~

 

$ python logging_level_example.py debug
DEBUG:root:This is a debug message
INFO:root:This is a info message
WARNING:root:This is a warning message
ERROR:root:This is a error message
CRITICAL:root:This is a critical error message
 
$ python logging_level_example.py info
INFO:root:This is a info message
WARNING:root:This is a warning message
ERROR:root:This is a error message
CRITICAL:root:This is a critical error message
 
$ python logging_level_example.py warning
WARNING:root:This is a warning message
ERROR:root:This is a error message
CRITICAL:root:This is a critical error message
 
$ python logging_level_example.py error
ERROR:root:This is a error message
CRITICAL:root:This is a critical error message
 
$ python logging_level_example.py critical
CRITICAL:root:This is a critical error message

logging模塊包括logger,handler,filter和formatter。
Logger.setLevel()設置日志級別

Logger.addFilter()

Logger.removeFilter()

Logger.debug()

Logger.info()

Logger.warning()

Logger.error()

Logger.critical()

Logger.exception() 

Logger.log()

--------------------------------------分割線 --------------------------------------

CentOS上源碼安裝Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡 

Copyright © Linux教程網 All Rights Reserved