import logging
# 日誌級別列表,預設為logging.warning
levels = (logging.notset, logging.debug, logging.info,
logging.warning, logging.error, logging.critical)
log_format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)-8s %(message)s'
# 輸出格式
date_format='%y-%m-%d %h:%m:%s'
# 日期格式
filename = "mylog.log"
# 日誌檔案
logging.basicconfig(level=levels[1], format=log_format, datefmt=date_format,
filename=filename, filemode='w')
logging.debug('this is a message'.format("debug"))
logging.info('this is an message'.format("info"))
logging.warning('this is a warning message')
logging.error('this is an error message')
logging.critical('this is a critical error message')
import logging
# 建立乙個logger
logger = logging.getlogger()
logger.setlevel(logging.debug)
# 建立filehandler,用於寫入日誌檔案
fh = logging.filehandler('test.log')
fh.setlevel(logging.debug)
# 建立streamhandler,用於輸出到控制台
ch = logging.streamhandler()
ch.setlevel(logging.debug)
# 定義handler的輸出格式
log_format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)-8s %(message)s'
formatter = logging.formatter(log_format)
fh.setformatter(formatter)
ch.setformatter(formatter)
# 給logger新增handler
logger.addhandler(fh)
logger.addhandler(ch)
# 記錄一條日誌
logger.info('test')
[1]
[2]
[3]
python logging模組簡介
logging模組是python內建的標準模組,主要用於輸出執行日誌,可以設定輸出日誌的等級 日誌儲存路徑 日誌檔案回滾等。相對於print,該模組具有可以決定在列印什麼級別的資訊和將資訊輸出放置在什麼地方的優點。配置logging的基本設定,並在控制台輸出 import logging loggi...
Python logging日誌模組
1.日誌的級別 日誌一共分成5個等級,從低到高分別是 1 debug 2.info 3.warning 4.error 5.critical說明 這5個等級,也分別對應5種打日誌的方法 debug info warning error critical。預設的是 warning,當在warning或...
python logging日誌模組
logging模組是python的乙個標準庫模組,由標準庫模組提供日誌記錄api的關鍵好處是所有python模組都可以使用這個日誌記錄功能。所以,你的應用日誌可以將你自己的日誌資訊與來自第三方模組的資訊整合起來。1.日誌級別 logging模組預設定義了以下幾個日誌等級,開發應用程式或部署開發環境時...