1
我正在爲我的包設置記錄器。Python將過濾級別記錄到流中
我有一個簡單的模塊,只是啓動某些日誌處理程序和格式。
- 文件處理器
- 控制檯處理程序(拆分成標準錯誤和標準輸出)
- 我也有設置一些格式和過濾器。我在過濾器上遇到問題。
- 我想有2個處理器,一個過濾出 INFO和DEBUG消息(stderr流)和其他過濾掉警告及以上(離開INFO和DEBUG)
我不知道我建立了我的過濾器類correctly.I得到這個錯誤,這是不是很清楚,我:
TypeError: unbound method filter() must be called with STDErrFilter instance as first argument (got LogRecord instance instead)
這裏是我的代碼,並在此先感謝!:
import os
import sys
import logging
import tempfile
class STDOutFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.INFO | record.levelno == logging.DEBUG
class STDErrFilter(logging.Filter):
def filter(self, record):
return not record.levelno == logging.INFO | record.levelno == logging.DEBUG
def start_logbook(log_path=None, log_formatting=None, std_formatting=None, debug=False, clear_previous=True,):
log_level = logging.INFO if not debug else logging.DEBUG
stdout_level = logging.INFO if not debug else logging.DEBUG
logger = logging.getLogger()
logger.setLevel(log_level)
log_path = log_path
if log_path:
folder, filename = os.path.split(log_path)
if folder and not os.path.exists(folder):
raise IOError("path {0} does not exist!".format(folder))
else:
# Setup Temp Log
log_path = tempfile.NamedTemporaryFile(prefix='tempLog'+'_log_', suffix='.log')
# Add File Handlers
file_handler = logging.FileHandler(log_path)
if clear_previous:
file_handler = logging.FileHandler(log_path, mode='w')
file_formatting = logging.Formatter('[%(asctime)s] %(levelname)-8s --- %(filename)s - %(funcName)s [%(lineno)d] - %(message)s', "%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatting)
# Add Console Handler
error_console_handler = logging.StreamHandler(stream=sys.stderr)
error_console_handler.setLevel(stdout_level)
error_console_handler.addFilter(STDErrFilter)
info_console_handler = logging.StreamHandler(stream=sys.stdout)
info_console_handler.setLevel(stdout_level)
info_console_handler.addFilter(STDOutFilter)
console_formatting = logging.Formatter('%(levelname)-8s --- %(message)s')
error_console_handler.setFormatter(console_formatting)
info_console_handler.setFormatter(console_formatting)
logger.addHandler(error_console_handler)
logger.addHandler(info_console_handler)
logger.addHandler(file_handler)
return {'log_filepath': os.path.abspath(log_path), 'handlers':[error_console_handler, info_console_handler, file_handler]}
if __name__ == '__main__':
info = start_logbook("test.log", debug=True)
log = logging.getLogger("Test Run")
log.debug("Debug Message goes Here")
log.info("INFO Message goes Here")
log.warning("WARNING Message goes Here")
log.error("ERROR Message goes Here")
log.critical("Critical Message goes Here")
log.exception(ValueError("Blarg"))
logging.shutdown(info['handlers'])