Python日志记录过滤器级别到流

时间:2015-05-12 18:56:12

标签: python logging

我正在为我的包设置一个记录器。

我有一个简单的模块,只需启动某些日志处理程序和格式。

  • 文件处理程序
  • 控制台处理程序(拆分为stderr和stdout)
  • 我还设置了一些格式和过滤器。我在使用过滤器时遇到了问题。
  • 我想拥有2个处理程序,一个用于过滤 out INFO和DEBUG Message(用于stderr流),另一个用于过滤WARNING及以上(保留INFO和DEBUG)

我不确定我是否正确设置了过滤器类。我收到此错误,这对我来说不是很清楚:

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'])

1 个答案:

答案 0 :(得分:1)

我弄明白了,愚蠢的错误。当我添加过滤器而不是传递实例化的对象时,我需要在代码中实例化类。

# Add Console Handler
error_console_handler = logging.StreamHandler(stream=sys.stderr)
error_console_handler.setLevel(stdout_level)
error_console_handler.addFilter(STDErrFilter()) # Problem was here I needed ()

info_console_handler = logging.StreamHandler(stream=sys.stdout)
info_console_handler.setLevel(stdout_level)
info_console_handler.addFilter(STDOutFilter()) # Problem was here! Needed ()