本文實例講述了Python3.6日志Logging模塊簡單用法。分享給大家供大家參考,具體如下:
Logging是一個很方便的模塊,用來打印日志
我直接列出一個最靈活的方法
# -*- coding:utf-8 -*-#!python3import logginglogger = logging.getLogger() # logging對象fh = logging.FileHandler("test.log") # 文件對象sh = logging.StreamHandler() # 輸出流對象fm = logging.Formatter('%(asctime)s-%(filename)s[line%(lineno)d]-%(levelname)s-%(message)s') # 格式化對象fh.setFormatter(fm) # 設置格式sh.setFormatter(fm) # 設置格式logger.addHandler(fh) # logger添加文件輸出流logger.addHandler(sh) # logger添加標準輸出流(std out)logger.setLevel(logging.DEBUG) # 設置從那個等級開始提示logger.debug("debug Test")logger.info("info Test")logger.warning("warning Test")logger.error("error Test")logger.critical("critical Test")
運行結果:
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python日志操作技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答