python logger日誌配置
阿新 • • 發佈:2018-12-21
1 self.logger = logging.getLogger(logName) # 建立logger例項 2 time = datetime.datetime.now() 3 logFilePath = os.path.abspath('../log/') + '/{}-{}-{}'.format(time.year, time.month, time.day) # 設定日誌檔案輸出路徑 4 self.check_mkdir(logFilePath) 5 logFile = logFilePath + '/out.log' 6 formatter = logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s') # 設定日誌輸出格式 7 loghanlder = logging.FileHandler(logFile, encoding='utf-8') # encoding要申明下,不然中文寫入中文檔案會亂碼 8 loghanlder.setFormatter(formatter) 9 loghanlder.setLevel(logging.DEBUG) #設定日誌輸出等級 10 logSt = logging.StreamHandler() 11 logSt.setFormatter(formatter) 12 self.logger.addHandler(loghanlder) 13 self.logger.addHandler(logSt)