python logging添加filter教程


Posted in Python onDecember 24, 2019

例子一

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解析xml文件操作实例
Oct 05 Python
python编程开发之日期操作实例分析
Nov 13 Python
在centos7中分布式部署pyspider
May 03 Python
python3实现全角和半角字符转换的方法示例
Sep 21 Python
python3.6+django2.0+mysql搭建网站过程详解
Jul 24 Python
python3.7 利用函数os pandas利用excel对文件名进行归类
Sep 29 Python
python文字转语音的实例代码分析
Nov 12 Python
Python如何安装第三方模块
May 28 Python
Python基础之教你怎么在M1系统上使用pandas
May 08 Python
Python中可变和不可变对象的深入讲解
Aug 02 Python
Qt自定义Plot实现曲线绘制的详细过程
Nov 02 Python
基于Python编写简易版的天天跑酷游戏的示例代码
Mar 23 Python
python打印异常信息的两种实现方式
Dec 24 #Python
numpy实现神经网络反向传播算法的步骤
Dec 24 #Python
python异常处理和日志处理方式
Dec 24 #Python
Python 音频生成器的实现示例
Dec 24 #Python
Python concurrent.futures模块使用实例
Dec 24 #Python
Python hmac模块使用实例解析
Dec 24 #Python
Python hashlib模块实例使用详解
Dec 24 #Python
You might like
php正则表达式使用的详细介绍
2013/04/27 PHP
学习PHP session的传递方式
2016/06/15 PHP
Javascript中自动切换焦点实现代码
2012/12/15 Javascript
JS取request值以及自动执行使用示例
2014/02/24 Javascript
js实现checkbox全选和反选示例
2014/05/01 Javascript
javascript消除window.close()的提示窗口
2015/05/20 Javascript
jQuery Validate表单验证深入学习
2015/12/18 Javascript
BootStrap点击下拉菜单项后显示一个新的输入框实现代码
2016/05/16 Javascript
Bootstrap弹出框(modal)垂直居中的问题及解决方案详解
2016/06/12 Javascript
走进AngularJs之过滤器(filter)详解
2017/02/17 Javascript
vue2.0获取自定义属性的值
2017/03/28 Javascript
JS简单实现点击跳转登陆邮箱功能的方法
2017/10/31 Javascript
vue axios整合使用全攻略
2018/05/24 Javascript
Vue的watch和computed方法的使用及区别介绍
2018/09/06 Javascript
微信小程序引用iconfont图标的方法
2018/10/22 Javascript
详解微信图片防盗链“此图片来自微信公众平台 未经允许不得引用”的解决方案
2019/04/04 Javascript
100行代码实现vue表单校验功能(小白自编)
2019/11/19 Javascript
vue父子组件间引用之$parent、$children
2020/05/20 Javascript
Vue 解决在element中使用$notify在提示信息中换行问题
2020/11/11 Javascript
详解Vue的异步更新实现原理
2020/12/22 Vue.js
[50:54]完美世界DOTA2联赛 GXR vs IO 第三场 11.07
2020/11/10 DOTA
python解析模块(ConfigParser)使用方法
2013/12/10 Python
python解析xml模块封装代码
2014/02/07 Python
python实现巡检系统(solaris)示例
2014/04/02 Python
python3实现全角和半角字符转换的方法示例
2017/09/21 Python
python生成九宫格图片
2018/11/19 Python
Python 的字典(Dict)是如何存储的
2019/07/05 Python
如何理解python对象
2020/06/21 Python
python中实现词云图的示例
2020/12/19 Python
从当地商店送来的杂货:Instacart
2018/08/19 全球购物
沙特阿拉伯家用电器和电子产品购物网站:Sheta and Saif
2020/04/03 全球购物
餐饮业的创业计划书范文
2013/12/26 职场文书
小学生操行评语
2014/04/22 职场文书
销售顾问工作计划书
2014/08/15 职场文书
荒岛余生观后感
2015/06/09 职场文书
Oracle 触发器trigger使用案例
2022/02/24 Oracle