python常用模組之logging (python3入門)
阿新 • • 發佈:2020-11-04
-
定義三種日誌輸出格式,日誌中可能用到的格式化串如下
-
%(name)s Logger的名字
-
%(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使用者輸出的訊息
-
-
強調:其中的%(name)s為getlogger時指定的名字
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]
-
日誌配置字典
LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard
-
使用
import settings # !!!強調!!! # 1、logging是一個包,需要使用其下的config、getLogger,可以如下匯入 # from logging import config # from logging import getLogger # 2、也可以使用如下匯入 import logging.config # 這樣連同logging.getLogger都一起匯入了,然後使用字首logging.config. # 3、載入配置 logging.config.dictConfig(settings.LOGGING_DIC) # 4、輸出日誌 logger1=logging.getLogger('使用者交易') logger1.info('小王給兒子李四轉賬3億人民幣') # logger2=logging.getLogger('專門的採集') # 名字傳入的必須是'專門的採集',與LOGGING_DIC中的配置唯一對應 # logger2.debug('專門採集的日誌')