logging 模塊

import logging包含 info,debug(默認不打印)error,warning,critical指定日誌打印級別把日誌輸出到文件裏logging.basicConfg(filename='xxx.log',level = logging.INFO)加上時間logging.basicConfig(filename='xxx.log',                         level = logging.INFO,                         format='%(asctime)s %(levelname)s %(message)s',                         datefmt='%m/%d/%Y %I:%M:%S %p')%(name)s logger的名字%(levelno)s  數字形式的日誌級別%(levelname)s 文本形式的日誌級別%(filename)s 調用日誌輸出函數的模塊的文件名%(funcName)s  調用日誌輸出函數的函數名%(lineno)d  調用日誌輸出函數的語句所在的代碼行%(module)s  調用模塊名%(thread)d   線程ID%(threadName)s 線程名%(process)d 進程ID%(message)s 用戶輸出的消息``````````````````````````````````````````````````````````````````````````````````````````````````````logger = logging.getLogger('TEST-LOG')#常見一個logger#返回一個logger實例,若是沒有指定name,返回root logger。只要name相同,返回的logger實例都是同一個並且只有一個,即name和logger實例是一一對應的。這意味着,無需把logger實例在各個模塊中傳遞。只要知道name,就能獲得同一個logger實例logger.setLevel(logging.DEBUG)#指定日誌輸出的等級ch = logging.StreamHandler()#屏幕輸出ch.setLevel(logging.DEBUG)#指定日誌輸出的等級fh = logging.FileHandler('ACCLOG')#fh = handlers.TimedRotatingFileHandler#截斷#輸出到文件acclogfh.setLevel(logging.ERROR)#指定日誌輸出的等級formatter = logging.Formatter('%(acctimes) - %(name)s - %(levelname)s - %(message)s')#定義日誌格式ch.setFormatter(formatter)fh.setFormatter(formatter)#設置格式logger.addHandler(fh)logger.addHandler(ch)#加入handlerlogger.warning('xxxx')`````````````````````````````````````````````````````````````````````````````````````````````````````
相關文章
相關標籤/搜索