python logging添加filter教程

yipeiwu_com5年前Python基础

例子一

def filter(self, record):
    """Our custom record filtering logic.
    Built-in filtering logic (via logging.Filter) is too limiting.
    """
    if not self.filters:
      return True
    matched = False
    rname = record.name # shortcut
    for name in self.filters:
      if rname == name or rname.startswith(name+'.'):
        matched = True
    return matched

例子二

def _create_log_handlers(stream):
  """Create and return a default list of logging.Handler instances.
  Format WARNING messages and above to display the logging level, and
  messages strictly below WARNING not to display it.
  Args:
   stream: See the configure_logging() docstring.
  """
  # Handles logging.WARNING and above.
  error_handler = logging.StreamHandler(stream)
  error_handler.setLevel(logging.WARNING)
  formatter = logging.Formatter("%(levelname)s: %(message)s")
  error_handler.setFormatter(formatter)
 
  # Create a logging.Filter instance that only accepts messages
  # below WARNING (i.e. filters out anything WARNING or above).
  non_error_filter = logging.Filter()
  # The filter method accepts a logging.LogRecord instance.
  non_error_filter.filter = lambda record: record.levelno < logging.WARNING
 
  non_error_handler = logging.StreamHandler(stream)
  non_error_handler.addFilter(non_error_filter)
  formatter = logging.Formatter("%(message)s")
  non_error_handler.setFormatter(formatter)
 
  return [error_handler, non_error_handler]

例子三

def _default_handlers(stream):
  """Return a list of the default logging handlers to use.
  Args:
   stream: See the configure_logging() docstring.
  """
  # Create the filter.
  def should_log(record):
    """Return whether a logging.LogRecord should be logged."""
    # FIXME: Enable the logging of autoinstall messages once
    #    autoinstall is adjusted. Currently, autoinstall logs
    #    INFO messages when importing already-downloaded packages,
    #    which is too verbose.
    if record.name.startswith("webkitpy.thirdparty.autoinstall"):
      return False
    return True
 
  logging_filter = logging.Filter()
  logging_filter.filter = should_log
 
  # Create the handler.
  handler = logging.StreamHandler(stream)
  formatter = logging.Formatter("%(name)s: [%(levelname)s] %(message)s")
  handler.setFormatter(formatter)
  handler.addFilter(logging_filter)
 
  return [handler]

以上这篇python logging添加filter教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python输入二维数组方法

前不久对于Python输入二维数组有些不解,今日成功尝试,记以备忘。这里以输入1-9,3*3矩阵为例 n=int(input()) line=[[0]*n]*n for i in r...

Python引用(import)文件夹下的py文件的方法

Python引用(import)文件夹下的py文件的方法

Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行...

pycharm重命名文件的方法步骤

pycharm重命名文件的方法步骤

使用pycharm的时候,有时需要重命名文件,该怎么操作呢?下面小编给大家演示一下。 首先准备一个要重命名的文件,如下图所示 接着右键单击选择Refactor选项,如下图所示 然后在...

Python闭包之返回函数的函数用法示例

Python闭包之返回函数的函数用法示例

闭包(closure)不是什么可怕的东西。如果用对了地方,它们其实可以很强大。闭包就是由其他函数动态生成并返回的函数,通俗地讲,在一个函数的内部,还有一个“内层”的函数,这个“内层”的函...

使用python为mysql实现restful接口

最近在做游戏服务分层的时候,一直想把mysql的访问独立成一个单独的服务DBGate,原因如下: 请求收拢到DBGate,可以使DBGate变为无状态的,方便横向扩展 当请求量或者存储量...