python logging添加filter教程
更新時(shí)間:2019年12月24日 17:18:15 作者:99zhenzhen
今天小編就為大家分享一篇python logging添加filter教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
例子一
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教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
np.mean()和np.std()函數(shù)的具體使用
本文主要介紹了np.mean()和np.std()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python中input()函數(shù)的用法實(shí)例小結(jié)
我們編寫的大部分程序,都需要讀取輸入并對(duì)其進(jìn)行處理,而基本的輸入操作是從鍵盤鍵入數(shù)據(jù),Python從鍵盤鍵入數(shù)據(jù),大多使用其內(nèi)置的input()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python中input()函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2022-03-03
Python實(shí)現(xiàn)數(shù)通設(shè)備端口使用情況監(jiān)控實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)數(shù)通設(shè)備端口使用情況監(jiān)控的方法,涉及Python針對(duì)設(shè)備監(jiān)控的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
pycharm恢復(fù)默認(rèn)設(shè)置或者是替換pycharm的解釋器實(shí)例
今天小編就為大家分享一篇pycharm恢復(fù)默認(rèn)設(shè)置或者是替換pycharm的解釋器實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10

