Python自己帶有logging模塊,其默認支持直接輸出到控制檯(屏幕),或者經過配置輸出到文件中。同時支持TCP、HTTP、GET/POST、SMTP、Socket等協議,將日誌信息發送到網絡等等。python
日誌級別大小關係爲:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,固然也能夠本身定義日誌級別。服務器
沒有配置logging時,日誌會直接輸出到控制檯網絡
import logging if __name__ == "__main__": logging.debug("hello debug") logging.info("hello info") logging.warning("hello warning") logging.error("hello error")
輸出結果:socket
WARNING:root:hello warning ERROR:root:hello error
是否是少了上面兩個? 由於默認狀況下,logging的級別爲warning.函數
經過logging.basicConfig函數作相關配置:.net
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',) if __name__ == "__main__": logging.debug("hello debug") logging.info("hello info") logging.warning("hello warning") logging.error("hello error")
輸出爲:線程
2017-03-28 12:22:55,052 test.py[line:8] DEBUG hello debug 2017-03-28 12:22:55,053 test.py[line:9] INFO hello info 2017-03-28 12:22:55,053 test.py[line:10] WARNING hello warning 2017-03-28 12:22:55,054 test.py[line:11] ERROR hello error
能夠指定日期格式。python中時間日期格式化符號: >%y 兩位數的年份表示(00-99) %Y 四位數的年份表示(000-9999) %m 月份(01-12) %d 月內中的一天(0-31) %H 24小時制小時數(0-23) %I 12小時制小時數(01-12) %M 分鐘數(00=59) %S 秒(00-59) %a 本地簡化星期名稱 %A 本地完整星期名稱 %b 本地簡化的月份名稱 %B 本地完整的月份名稱 %c 本地相應的日期表示和時間表示 %j 年內的一天(001-366) %p 本地A.M.或P.M.的等價符 %U 一年中的星期數(00-53)星期天爲星期的開始 %w 星期(0-6),星期天爲星期的開始 %W 一年中的星期數(00-53)星期一爲星期的開始 %x 本地相應的日期表示 %X 本地相應的時間表示 %Z 當前時區的名稱 %% %號自己debug
代碼修改以下:日誌
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %A %H:%M:%S') if __name__ == "__main__": logging.debug("hello debug"); logging.info("hello info"); logging.warning("hello warning"); logging.error("hello error");
經過logging.basicConfig來配置輸出文件路徑:code
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %A %H:%M:%S', filename='python.log', filemode='w') if __name__ == "__main__": logging.debug("hello debug"); logging.info("hello info"); logging.warning("hello warning"); logging.error("hello error");
輸出結果:
2017-03-28 Tuesday 12:33:29 test.py[line:10] DEBUG hello debug 2017-03-28 Tuesday 12:33:29 test.py[line:11] INFO hello info 2017-03-28 Tuesday 12:33:29 test.py[line:12] WARNING hello warning 2017-03-28 Tuesday 12:33:29 test.py[line:13] ERROR hello error
###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被忽略。
經過handler來讓日誌能夠即輸出到文件中又輸出到控制檯上。
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %A %H:%M:%S', filename='python.log', filemode='w') console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter('%(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) if __name__ == "__main__": logging.debug("hello debug"); logging.info("hello info"); logging.warning("hello warning"); logging.error("hello error");
控制檯輸出內容:
INFO hello info WARNING hello warning ERROR hello error
文件中輸出內容:
2017-03-28 Tuesday 12:38:19 test.py[line:16] DEBUG hello debug 2017-03-28 Tuesday 12:38:19 test.py[line:17] INFO hello info 2017-03-28 Tuesday 12:38:19 test.py[line:18] WARNING hello warning 2017-03-28 Tuesday 12:38:19 test.py[line:19] ERROR hello error
當日志一直向一個文件中輸出,會致使文件太大。因此有時候但願日誌文件滾動生成多個文件。
import logging from logging.handlers import RotatingFileHandler logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %A %H:%M:%S') rfhandler = RotatingFileHandler('python.log', maxBytes=10*1024*1024, backupCount=5) rfhandler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') rfhandler.setFormatter(formatter) logging.getLogger('').addHandler(rfhandler) if __name__ == "__main__": logging.debug("hello debug"); logging.info("hello info"); logging.warning("hello warning"); logging.error("hello error");
當文件輸出超過1010241024=10M大小時,就會生成一個新的日誌文件。
RotatingFileHandler的參數:
logging的包括以下幾種handler方式:
因爲StreamHandler和FileHandler是經常使用的日誌處理方式,因此直接包含在logging模塊中,而其餘方式則包含在logging.handlers模塊中,
還有一種方式是不在代碼中對logging進行配置,而是寫到配置文件中。
#logger.conf ############################ [loggers] keys=root,example01,example02 [logger_root] level=DEBUG handlers=hand01,hand02 [logger_example01] handlers=hand01,hand02 qualname=example01 propagate=0 [logger_example02] handlers=hand01,hand03 qualname=example02 propagate=0 ############################ [handlers] keys=hand01,hand02,hand03 [handler_hand01] class=StreamHandler level=INFO formatter=form02 args=(sys.stderr,) [handler_hand02] class=FileHandler level=DEBUG formatter=form01 args=('python.log', 'a') [handler_hand03] class=handlers.RotatingFileHandler level=INFO formatter=form02 args=('python.log', 'a', 10*1024*1024, 5) ############################ [formatters] keys=form01,form02 [formatter_form01] format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s datefmt=%a, %d %b %Y %H:%M:%S [formatter_form02] format=%(name)-12s: %(levelname)-8s %(message)s datefmt=
在python使用以下:
import logging import logging.config logging.config.fileConfig("logger.conf") logger = logging.getLogger("example01") if __name__ == "__main__": logger.debug("hello debug"); logger.info("hello info"); logger.warning("hello warning"); logger.error("hello error");
日誌的輸出格式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: 打印日誌信息
工做中給的經常使用格式是:
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
這個格式能夠輸出日誌的打印時間,是哪一個模塊輸出的,輸出的日誌級別是什麼,以及輸入的日誌內容。
參考: http://blog.csdn.net/yatere/article/details/6655445 http://blog.csdn.net/z_johnny/article/details/50812878 http://blog.csdn.net/liuchunming033/article/details/39080457