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语言技巧之三元运算符使用介绍
Mar 04 Python
Python同时向控制台和文件输出日志logging的方法
May 26 Python
基于Python log 的正确打开方式
Apr 28 Python
python爬虫之模拟登陆csdn的实例代码
May 18 Python
Python 隐藏输入密码时屏幕回显的实例
Feb 19 Python
Python小白必备的8个最常用的内置函数(推荐)
Apr 03 Python
python进程池实现的多进程文件夹copy器完整示例
Nov 27 Python
python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法
Feb 26 Python
Python图片处理模块PIL操作方法(pillow)
Apr 07 Python
对python中arange()和linspace()的区别说明
May 03 Python
python操作链表的示例代码
Sep 27 Python
python 利用matplotlib在3D空间绘制二次抛物面的案例
Feb 06 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 Session存储到Redis的方法
2013/11/04 PHP
在PHP程序中使用Rust扩展的方法
2015/07/03 PHP
PHP图片加水印实现方法
2016/05/06 PHP
php+ajax实现文件切割上传功能示例
2020/03/03 PHP
《JavaScript DOM 编程艺术》读书笔记之JavaScript 简史
2015/01/09 Javascript
JavaScript基本类型值-Number类型
2017/02/24 Javascript
详解webpack进阶之插件篇
2017/07/06 Javascript
Vue 父子组件数据传递的四种方式( inheritAttrs + $attrs + $listeners)
2018/05/04 Javascript
Vue 页面状态保持页面间数据传输的一种方法(推荐)
2018/11/01 Javascript
Python实现过滤单个Android程序日志脚本分享
2015/01/16 Python
Django中URLconf和include()的协同工作方法
2015/07/20 Python
Python获取linux主机ip的简单实现方法
2016/04/18 Python
Python实现简单的语音识别系统
2017/12/13 Python
Python实现的购物车功能示例
2018/02/11 Python
Python基于pycrypto实现的AES加密和解密算法示例
2018/04/10 Python
Python 基于wxpy库实现微信添加好友功能(简洁)
2019/11/29 Python
Python调用scp向服务器上传文件示例
2019/12/22 Python
Python3 使用selenium插件爬取苏宁商家联系电话
2019/12/23 Python
浅谈tensorflow之内存暴涨问题
2020/02/05 Python
Java Unsafe类实现原理及测试代码
2020/09/15 Python
css3图片边框border-image的用法
2017/06/30 HTML / CSS
利物浦足球俱乐部官方网上商店:Liverpool FC Official Store
2018/01/13 全球购物
爱尔兰领先的在线体育用品零售商:theGAAstore
2018/04/16 全球购物
美国在线轮胎零售商:SimpleTire
2019/04/08 全球购物
Expedia西班牙:预订酒店、机票、旅行和廉价度假套餐
2019/04/10 全球购物
英国床垫和床架购物网站:Bedman
2019/11/04 全球购物
C面试题
2015/10/08 面试题
财务主管的岗位职责
2013/12/30 职场文书
财务人员的自我评价范文
2014/03/03 职场文书
买房协议书
2014/04/11 职场文书
服务理念标语
2014/06/18 职场文书
小学班主任事迹材料
2014/12/17 职场文书
结婚喜宴祝酒词
2015/08/10 职场文书
银行求职信怎么写
2019/06/20 职场文书
Python提取PDF指定内容并生成新文件
2021/06/09 Python
Nginx实现负载均衡的项目实践
2022/03/18 Servers