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读取浮点数和读取文本文件示例
May 06 Python
Python实现字符串反转的常用方法分析【4种方法】
Sep 30 Python
Python实现两款计算器功能示例
Dec 19 Python
详谈Python 窗体(tkinter)表格数据(Treeview)
Oct 11 Python
python 动态生成变量名以及动态获取变量的变量名方法
Jan 20 Python
python实现车牌识别的示例代码
Aug 05 Python
python连接PostgreSQL数据库的过程详解
Sep 18 Python
python线程join方法原理解析
Feb 11 Python
python输出pdf文档的实例
Feb 13 Python
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
Feb 24 Python
解决Jupyter无法导入已安装的 module问题
Apr 17 Python
Python趣味挑战之用pygame实现简单的金币旋转效果
May 31 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蜘蛛统计插件只要有mysql就可用
2010/10/12 PHP
PHP常用特殊运算符号和函数总结(php新手入门必看)
2013/02/02 PHP
fromCharCode和charCodeAt 方法
2006/12/27 Javascript
不错的JS中变量相关的细节分析
2007/08/13 Javascript
JS常用正则表达式总结
2013/11/12 Javascript
利用window.name实现windowStorage代码分享
2014/01/02 Javascript
javascript原型模式用法实例详解
2015/06/04 Javascript
js结合正则实现国内手机号段校验
2015/06/19 Javascript
简单了解Backbone.js的Model模型以及View视图的源码
2016/02/14 Javascript
AngularJS基础 ng-srcset 指令简单示例
2016/08/03 Javascript
javascript编程开发中取色器及封装$函数用法示例
2017/08/09 Javascript
React中jquery引用的实现方法
2017/09/12 jQuery
BootstrapTable加载按钮功能实例代码详解
2017/09/22 Javascript
webpack构建的详细流程探底
2018/01/08 Javascript
使用webpack3.0配置webpack-dev-server教程
2018/05/29 Javascript
深入浅析var,let,const的异同点
2018/08/07 Javascript
elementUI 设置input的只读或禁用的方法
2018/10/30 Javascript
详解vue中使用transition和animation的实例代码
2020/12/12 Vue.js
[02:19]2018年度DOTA2最佳核心位选手-完美盛典
2018/12/17 DOTA
python 文件和路径操作函数小结
2009/11/23 Python
Python中用函数作为返回值和实现闭包的教程
2015/04/27 Python
Flask框架的学习指南之用户登录管理
2016/11/20 Python
python 实现tar文件压缩解压的实例详解
2017/08/20 Python
python3模块smtplib实现发送邮件功能
2018/05/22 Python
python 借助numpy保存数据为csv格式的实现方法
2018/07/04 Python
很酷的python表白工具 你喜欢我吗
2019/04/11 Python
pytorch实现Tensor变量之间的转换
2020/02/17 Python
Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性(推荐)
2020/07/03 Python
美国新娘礼品店:The Paisley Box
2020/09/08 全球购物
小学毕业感言500字
2014/02/28 职场文书
公司会议策划方案
2014/05/17 职场文书
雷人标语集锦
2014/06/19 职场文书
2015年出纳个人工作总结
2015/04/02 职场文书
党员承诺书格式范文
2015/04/28 职场文书
企业战略合作意向书
2015/05/08 职场文书
Windows 11上手初体验:任务栏和开始菜单等迎来大改
2021/11/21 数码科技