今天是python的logging庫,這是一個典型的日誌庫,用法簡單實在。html
首先是最簡單的python
import logging logging.debug("this is debug") logging.info("this is info") logging.warning("this is warning") logging.error("this is error") logging.critical("this is critical") # 輸出 # this is warning # this is error # this is critical # 之因此這裏沒有前兩條是由於logging默認的日誌級別是warning,只有大於等於這個日誌級別的日誌纔會輸出 # 這邊須要從新鍵入代碼,不然前面的日誌庫的對象是不會被清空的,會出現仍是輸出上面3條,要從新載入IPython # 這時候咱們纔會有以下的結果 import logging logging.basicConfig(level=logging.DEBUG) logging.debug("this is debug") logging.info("this is info") logging.warning("this is warning") logging.error("this is error") logging.critical("this is critical") # 輸出 # this is debug # this is info # this is warning # this is error # this is critical
接下來是logging中basicConfig的一些參數使用api
# filename 輸出到文件的名字 # filemode 輸出文件的格式,a,w,簡單的兩個 # format 基本語句的格式 # datefmt 日期的格式 # level 日誌的級別 # stream 初始化的流 import logging logging.basicConfig(filename="first.log",level=logging.DEBUG,filemode="a",format="%(asctime)s %(name)s %(levelname)s %(message)s",datefmt="%Y/%m/%d %H:%M:%S") # format解釋,如下爲幾個經常使用的format的關鍵字 %(key_words)s 都是基於這樣形式包裹的 # asctime 時間 # created 毫秒值 # filename 日誌在的地方 # funcName 日誌所在函數的名字 # levelname 日誌的級別 # levelno 日誌的級別,用數字表示 # lineno 日誌所在的行(運行文件的位置) # module 模塊名字 # msecs 毫秒值 # message 信息 # name 當前logger的名字 # pathname 日誌運行文件的全路徑 # process 進程id # processName 進程名字(若是有的話) # thread 線程id # threadName 線程名字 # datefmt簡單如下幾個,須要更復雜的查python api去 # %Y 年, %m 月, %d 日 ,%H 小時, %M 分鐘 %S 秒 logging.debug("this is debug") logging.info("this is info") logging.warning("this is warning") logging.error("this is error") logging.critical("this is critical") # 結果出如今first.log上 # 2017/02/21 15:58:13 root DEBUG this is debug # 2017/02/21 15:58:13 root INFO this is info # 2017/02/21 15:58:13 root WARNING this is warning # 2017/02/21 15:58:13 root ERROR this is error # 2017/02/21 15:58:13 root CRITICAL this is critical
接下來就是稍微複雜的logger,handler,formatter函數
import logging logger = logging.getLogger("sample") # 注意全部的handler都要和logger的日誌級別相同,不然會出現冗餘現象哦,即打印一條出現不少條 logger.setLevel(logging.DEBUG) sh = logging.StreamHandler() # 控制檯輸出流的handler sh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(name) %(asctime)s %(levelname)s %(message)s") sh.setFormatter(formatter) fh = logging.FileHandler("sample.log",mode="a",encoding="utf-8",delay=0.5) # 文件流handler fh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s %(asctime)s %(message)s") fh.setFormatter(formatter) logger.addHandler(sh) # 一個logger能夠有多個的handler logger.addHandler(fh) logger.debug("this is debug") logger.info("this is info") logger.error("this is error") logger.warning("this is warning") logger.critical("this is critical") # 輸出 # sample 2017-02-21 15:53:51,546 DEBUG this is debug # sample 2017-02-21 15:53:51,550 INFO this is info # sample 2017-02-21 15:53:51,552 ERROR this is error # sample 2017-02-21 15:53:51,553 WARNING this is warning # sample 2017-02-21 15:53:51,555 CRITICAL this is critical
最後的更復雜的日誌,就本身查看官方文檔了,例如日誌的配置仍是回滾日誌等等,都很簡單this