參考:http://blog.csdn.net/zyz511919766/article/details/25136485python
logging模塊函數
日誌有5個級別,分別爲debug,info,warning,error,critical,對應的級別數字分別爲10,20,30,40,50spa
將日誌標準輸出到屏幕上.net
#!/usr/bin/env python # -*- coding:utf-8 -*- import logging logging.debug("welcome to akon debug system") logging.info("welcome to akon info system") logging.warning("welcome to akon warning system") logging.error("welcome to akon error system") logging.critical("welcome to akon critical system")
輸出結果:線程
WARNING:root:welcome to akon warning system
ERROR:root:welcome to akon error system
CRITICAL:root:welcome to akon critical system
由結果可見,默認狀況下只會打印等於或高於warning級別的日誌。debug
將日誌以自定義的格式保存到文件中日誌
#!/usr/bin/env python # -*- coding:utf-8 -*- import logging logging.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='access.log', filemode='a') logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
查看access.log文件內容:code
Tue, 22 Mar 2016 17:00:42 log.py[line:18] DEBUG debug message Tue, 22 Mar 2016 17:00:42 log.py[line:19] INFO info message Tue, 22 Mar 2016 17:00:42 log.py[line:20] WARNING warning message Tue, 22 Mar 2016 17:00:42 log.py[line:21] ERROR error message Tue, 22 Mar 2016 17:00:42 log.py[line:22] CRITICAL critical message
用logging.baseConfig()函數定義日誌輸出格式,參數以下:orm
filename:用指定的文件名建立FiledHandler,這樣日誌會被存儲在指定的文件中。
filemode:文件打開方式,在指定了filename時使用這個參數,默認值爲「a」還可指定爲「w」。
format:指定handler使用的日誌顯示格式。
datefmt:指定日期時間格式。
level:設置rootlogger(後邊會講解具體概念)的日誌級別
stream:用指定的stream建立StreamHandler。能夠指定輸出到sys.stderr,sys.stdout或者文件,默認爲sys.stderr。若同時列出了filename和stream兩個參數,則stream參數會被忽略。
format參數中可能用到的格式化串:blog
%(name)s logger的名字,默認爲root %(levelno)s 數字形式的日誌級別 %(levelname)s 文本形式的日誌級別 %(pathname)s 調用日誌輸出函數的模塊的完整路徑名(可能沒有) %(filename)s 調用日誌輸出函數的模塊的文件名 %(module)s 調用日誌輸出函數的模塊名 %(funcName)s 調用日誌輸出函數的函數名 %(lineno)d 調用日誌輸出函數的語句所在的代碼行 %(created)f 當前時間,用UNIX標準的表示時間的浮點數表示 %(relativeCreated)d 輸出日誌信息時的,自Logger建立以來的毫秒數 %(asctime)s 字符串形式的當前時間。默認格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒 %(thread)d 線程ID(可能沒有) %(threadName)s 線程名(可能沒有) %(process)d 進程ID(可能沒有) %(message)s用戶輸出的消息
將日誌同時輸出到屏幕和保存到文件中
#!/usr/bin/env python # -*- coding:utf-8 -*- import logging # create logger logger = logging.getLogger("mylog") logger.setLevel(logging.DEBUG) # create formatter建立日誌輸出格式 formatter = logging.Formatter(format("%(asctime)s %(name)s %(levelname)s %(message)s"), datefmt="%a %d %b %Y,%H:%M:%S") # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.INFO) # create file handler and set level to debug fh = logging.FileHandler("mytestlog.log") fh.setLevel(logging.WARNING) # add formatter to ch and fh把輸出格式賦給ch和fh ch.setFormatter(formatter) fh.setFormatter(formatter) # add ch and fh to logger logger.addHandler(ch) logger.addHandler(fh) logger.debug("{} debug message".format("akon")) logger.info("{} info message".format("akon")) logger.warning("{} warning message".format("akon")) logger.error("{} error message".format("akon")) logger.critical("{} critical message".format("akon"))
屏幕輸出爲:
Tue 22 Mar 2016,18:18:36 mylog INFO akon info message Tue 22 Mar 2016,18:18:36 mylog WARNING akon warning message Tue 22 Mar 2016,18:18:36 mylog ERROR akon error message Tue 22 Mar 2016,18:18:36 mylog CRITICAL akon critical message
文件保存爲:
Tue 22 Mar 2016,18:18:36 mylog WARNING akon warning message Tue 22 Mar 2016,18:18:36 mylog ERROR akon error message Tue 22 Mar 2016,18:18:36 mylog CRITICAL akon critical message