python logging添加filter教程
作者:99zhenzhen 时间:2022-08-21 00:36:43
例子一
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]
来源:https://blog.csdn.net/kelindame/article/details/70312756
标签:python,logging,filter
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
聊聊golang中多个defer的执行顺序
2023-09-03 09:23:22
网页中空格的烦恼
2011-04-28 09:26:00
Warning: require(): open_basedir restriction in effect,目录配置open_basedir报错问题分析
2023-06-02 23:28:18
![](https://img.aspxhome.com/file/2023/2/55362_0s.png)
Python使用win32com.client的方法示例
2021-03-22 14:32:48
使用ACCESS做网络版程序的四种解决方案
2009-01-14 16:22:00
网马解密大讲堂——网马解密中级篇(Freshow工具使用方法)
2009-09-16 15:09:00
![](https://img.aspxhome.com/file/UploadPic/20099/16/freshow-13s.jpg)
mysql导入导出命令
2011-07-04 11:28:50
也谈网页圆角的背景图法
2009-03-19 14:09:00
![](https://img.aspxhome.com/file/UploadPic/20093/19/k2009313222051-53s.gif)
Python实现带图形界面的炸金花游戏
2021-06-21 15:41:56
![](https://img.aspxhome.com/file/2023/2/70032_0s.gif)
基于Python实现经典植物大战僵尸游戏
2021-01-28 11:17:20
![](https://img.aspxhome.com/file/2023/8/84038_0s.jpg)
django美化后台django-suit的安装配置操作
2021-12-19 23:13:25
设计和企业文化
2009-03-28 10:35:00
python检查字符串是否是正确ISBN的方法
2022-05-10 14:54:01
如何利用Image Data Type在主页中显示图形?
2010-01-01 15:13:00
python实现扫雷游戏的示例
2023-04-14 12:07:21
![](https://img.aspxhome.com/file/2023/6/84976_0s.png)
用Python的Django框架完成视频处理任务的教程
2022-05-07 05:38:25
Thinkphp5.0 框架的请求方式与响应方式分析
2023-11-15 00:07:09
JavaScript 获取客户端计算机硬件及系统信息
2009-01-13 17:59:00
![](https://img.aspxhome.com/file/UploadPic/20091/13/200901081050518738-49s.gif)
Oracle 当前用户下所有表的记录总数
2009-07-14 21:34:00
Python笔记之工厂模式
2022-11-07 17:58:47