跑程式猶如走人生,有些美好的、不美好的事需要記錄下來,慢慢斟酌~
logging模組由此而生,記錄那些你想記錄的事~
一、將日誌列印到螢幕
import logginglogging.debug('this is a bug information')logging.info('this is a info information')logging.warning('this is a warning information')logging.error('this is a error information')logging.critical('this is a critical information')# 螢幕上列印# WARNING:root:this is a warning information# ERROR:root:this is a error information# CRITICAL:root:this is a critical information 一般的,記錄層級大小關係為:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,當然也可以自己定義記錄層級。而且,預設情況下,logging將日誌列印到螢幕,記錄層級為WARNING,所以,我們寫的debug和info被忽略,不會列印。
二、將日誌列印到檔案
1. 利用logging.basicConfig()來配置
import logginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='myapp.log', filemode='w')logging.debug('this is a bug information')logging.info('this is a info information')logging.warning('this is a warning information')logging.error('this is a error information')logging.critical('this is a critical information')# myapp.log檔案裡列印#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:11] DEBUG this is a bug information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:12] INFO this is a info information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:13] WARNING this is a warning information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:14] ERROR this is a error information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:15] CRITICAL this is a critical information logging.basicConfig函數各參數:
filename: 指定記錄檔名,如果沒有指定就列印到螢幕
filemode: 和file函數意義相同,指定記錄檔的開啟模式,'w'或'a'
format: 指定輸出的格式和內容,format可以輸出很多有用資訊,如上例所示:
%(levelno)s: 列印記錄層級的數值
%(levelname)s: 列印記錄層級名稱
%(pathname)s: 列印當前執行程式的路徑,其實就是sys.argv[0]
%(filename)s: 列印當前執行程式名
%(funcName)s: 列印日誌的當前函數
%(lineno)d: 列印日誌的當前行號
%(asctime)s: 列印日誌的時間
%(thread)d: 列印線程ID
%(threadName)s: 列印線程名稱
%(process)d: 列印進程ID
%(message)s: 列印日誌資訊
datefmt: 指定時間格式,同time.strftime()
level: 設定記錄層級,預設為logging.WARNING
stream: 指定將日誌的輸出資料流,可以指定輸出到sys.stderr,sys.stdout或者檔案,預設輸出到sys.stderr,當stream和filename同時指定時,stream被忽略
2. 利用FileHandler來配置
import loggingfrom logging import FileHandlerLOG_PATH = '/home/tops/adms-agent/logs'logging.basicConfig(level=logging.DEBUG)file_time_handler = FileHandler(LOG_PATH+'/adms-agent.log')file_time_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - {%(filename)s:%(funcName)s:%(lineno)d}: %(message)s"))file_time_handler.suffix = "%Y-%m-%d.log"log = logging.getLogger("adms-agent")log.addHandler(file_time_handler)logging.debug('this is a bug information')logging.info('this is a info information')logging.warning('this is a warning information')logging.error('this is a error information')logging.critical('this is a critical information')
以上只是舉個例子用FileHandler來把日誌寫到檔案,其實有很多種日誌重新導向方法,例如:
logging.StreamHandler: 日誌輸出到流,可以是sys.stderr、sys.stdout或者檔案
logging.FileHandler: 日誌輸出到檔案
日誌復原方式,實際使用時用RotatingFileHandler和TimedRotatingFileHandler
logging.handlers.BaseRotatingHandler
logging.handlers.RotatingFileHandler
logging.handlers.TimedRotatingFileHandler
logging.handlers.SocketHandler: 遠程輸出日誌到TCP/IP sockets
logging.handlers.DatagramHandler: 遠程輸出日誌到UDP sockets
logging.handlers.SMTPHandler: 遠程輸出日誌到郵件地址
logging.handlers.SysLogHandler: 日誌輸出到syslog
logging.handlers.NTEventLogHandler: 遠程輸出日誌到Windows NT/2000/XP的事件記錄
logging.handlers.MemoryHandler: 日誌輸出到記憶體中的制定buffer
logging.handlers.HTTPHandler: 通過"GET"或"POST"遠程輸出到HTTP伺服器 三、logging是安全執行緒的