logging模塊初識

日誌級別


import logging

logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")
#級別爲debug info不會打印出信息 由於默認級別不夠,默認級別是WARNING,可使用打印到屏幕上的方式記錄,也能夠記錄到文件中。


logging的配置

經過logging.basicConfig能夠靈活的對log進行配置參數
logging.basicConfig(level=logging.DEBUG,#選擇級別                    format='%(asctime)s  %(filename)s  [line:%(lineno)d]  %(levelname)s  %(message)s',                    #變量依次爲:時間,運行的文件名(不是日誌名),在第幾行記錄的,級別名字,記錄的內容。這些變量並非必須都有的。                    datefmt='%Y %H:%M:%S',#時間格式(根據本身來選擇)                    filename='./test.log',#日誌名字                    filemode='w') #文件模式(w,a)#還有許多日誌可用參數上面只是列出經常使用之處logging.debug("debug message")logging.info("info message")logging.warning("warning message")logging.error("error message")logging.critical("critical message")
相關文章
相關標籤/搜索