標籤:%s pass pre 平台 roc div filename ons col
# !/user/bin/python# -*- coding: utf-8 -*-‘‘‘subprocess : 需要在linux平台上測試 shelllogging‘‘‘import logging# 將日誌輸出在檔案裡# logging.basicConfig(filename="app.log", level=logging.DEBUG)logging.basicConfig(filename="app.log", level=logging.WARNING, format=‘%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s‘, datefmt=‘%m/%d/%Y %I:%M:%S %p‘) # 在日誌上加上時間. %p代表pm. TODO 為什麼沒打出行數?logging.debug("test debug")logging.info("test info")logging.error("test error")logging.warning("User [alex] attempted wrong password more than 3 times")# 同時將日誌列印在螢幕上並輸出在檔案裡# step 1, create loggerlogger = logging.getLogger("TEST-LOG")logger.setLevel(logging.DEBUG)# step2, create console handler and set level to debugch=logging.StreamHandler()ch.setLevel(logging.DEBUG)# step3, create file handler and set level to warningfh = logging.FileHandler("process.log")fh.setLevel(logging.ERROR)# step3, define formatfh_formatter = logging.Formatter(‘%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s‘)ch_formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)fh.setFormatter(fh_formatter)ch.setFormatter(ch_formatter)# step4, connect handlers to loggerlogger.addHandler(fh)logger.addHandler(ch)logger.warning("ddddd")
Python logger /logging