本文整理汇总了Python中logging.Filter方法的典型用法代码示例。如果您正苦于以下问题:Python logging.Filter方法的具体用法?Python logging.Filter怎么用?Python logging.Filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging
的用法示例。
在下文中一共展示了logging.Filter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_stream_handlers
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def setup_stream_handlers(conf):
"""Setup logging stream handlers according to the options."""
class StdoutFilter(logging.Filter):
def filter(self, record):
return record.levelno in (logging.DEBUG, logging.INFO)
log.handlers = []
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.WARNING)
stdout_handler.addFilter(StdoutFilter())
if conf.debug:
stdout_handler.setLevel(logging.DEBUG)
elif conf.verbose:
stdout_handler.setLevel(logging.INFO)
else:
stdout_handler.setLevel(logging.WARNING)
log.addHandler(stdout_handler)
stderr_handler = logging.StreamHandler(sys.stderr)
msg_format = "%(levelname)s: %(message)s"
stderr_handler.setFormatter(logging.Formatter(fmt=msg_format))
stderr_handler.setLevel(logging.WARNING)
log.addHandler(stderr_handler)
示例2: test_logging_filters
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def test_logging_filters(sentry_init, capture_events):
sentry_init(integrations=[LoggingIntegration()], default_integrations=False)
events = capture_events()
should_log = False
class MyFilter(logging.Filter):
def filter(self, record):
return should_log
logger.addFilter(MyFilter())
logger.error("hi")
assert not events
should_log = True
logger.error("hi")
(event,) = events
assert event["logentry"]["message"] == "hi"
示例3: setup_logging
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def setup_logging(verbose=0, colors=False, name=None):
"""Configure console logging. Info and below go to stdout, others go to stderr.
:param int verbose: Verbosity level. > 0 print debug statements. > 1 passed to sphinx-build.
:param bool colors: Print color text in non-verbose mode.
:param str name: Which logger name to set handlers to. Used for testing.
"""
root_logger = logging.getLogger(name)
root_logger.setLevel(logging.DEBUG if verbose > 0 else logging.INFO)
formatter = ColorFormatter(verbose > 0, colors)
if colors:
colorclass.Windows.enable()
handler_stdout = logging.StreamHandler(sys.stdout)
handler_stdout.setFormatter(formatter)
handler_stdout.setLevel(logging.DEBUG)
handler_stdout.addFilter(type('', (logging.Filter,), {'filter': staticmethod(lambda r: r.levelno <= logging.INFO)}))
root_logger.addHandler(handler_stdout)
handler_stderr = logging.StreamHandler(sys.stderr)
handler_stderr.setFormatter(formatter)
handler_stderr.setLevel(logging.WARNING)
root_logger.addHandler(handler_stderr)
示例4: configure_logging
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def configure_logging(app: "sphinx.application.Sphinx") -> None:
"""Configure Sphinx's WarningHandler to handle (expected) missing include."""
import sphinx.util.logging
import logging
class WarnLogFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
"""Ignore warnings about missing include with "only" directive.
Ref: https://github.com/sphinx-doc/sphinx/issues/2150."""
if (
record.msg.startswith('Problems with "include" directive path:')
and "_changelog_towncrier_draft.rst" in record.msg
):
return False
return True
logger = logging.getLogger(sphinx.util.logging.NAMESPACE)
warn_handler = [x for x in logger.handlers if x.level == logging.WARNING]
assert len(warn_handler) == 1, warn_handler
warn_handler[0].filters.insert(0, WarnLogFilter())
示例5: test_filter
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def test_filter(self):
# Only messages satisfying the specified criteria pass through the
# filter.
filter_ = logging.Filter("spam.eggs")
handler = self.root_logger.handlers[0]
try:
handler.addFilter(filter_)
spam = logging.getLogger("spam")
spam_eggs = logging.getLogger("spam.eggs")
spam_eggs_fish = logging.getLogger("spam.eggs.fish")
spam_bakedbeans = logging.getLogger("spam.bakedbeans")
spam.info(self.next_message())
spam_eggs.info(self.next_message()) # Good.
spam_eggs_fish.info(self.next_message()) # Good.
spam_bakedbeans.info(self.next_message())
self.assert_log_lines([
('spam.eggs', 'INFO', '2'),
('spam.eggs.fish', 'INFO', '3'),
])
finally:
handler.removeFilter(filter_)
示例6: _resize_selection_box
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def _resize_selection_box(self, new_box):
import inspect # Let's get our hands dirty
stack = inspect.stack()
filename = os.path.basename(stack[1][1])
old_box = self.selectionTool.selectionBox()
msg = """
Filter "{0}" wants to resize the selection box
Origin: {1} -> {2}
Size: {3} -> {4}
Do you want to resize the Selection Box?""".format(
filename,
(old_box.origin[0], old_box.origin[1], old_box.origin[2]),
(new_box.origin[0], new_box.origin[1], new_box.origin[2]),
(old_box.size[0], old_box.size[1], old_box.size[2]),
(new_box.size[0], new_box.size[1], new_box.size[2])
)
result = ask(msg, ["Yes", "No"])
if result == "Yes":
self.selectionTool.setSelection(new_box)
return new_box
else:
return False
示例7: configure_filter
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def configure_filter(self, config):
"""Configure a filter from a dictionary."""
if '()' in config:
result = self.configure_custom(config)
else:
name = config.get('name', '')
result = logging.Filter(name)
return result
示例8: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def __init__(self, *args, **kwargs):
# Can't use super() because logging.Filter is an old-style class in py26
logging.Filter.__init__(self, *args, **kwargs)
self.warning_count = self.error_count = 0
示例9: _new_custom_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def _new_custom_logger(name='BiblioPixel',
fmt='%(levelname)s - %(module)s - %(message)s'):
logger = logging.getLogger(name)
formatter = logging.Formatter(fmt=fmt)
def add_handler(level, outfile):
class Filter(logging.Filter):
def filter(self, rec):
return rec.levelno == level
h = logging.StreamHandler(outfile)
h.setLevel(level)
h.addFilter(Filter())
h.setFormatter(formatter)
logger.addHandler(h)
if not logger.handlers:
logger.setLevel(INFO)
add_handler(FRAME, sys.stdout)
add_handler(DEBUG, sys.stdout)
add_handler(INFO, sys.stdout)
add_handler(WARNING, sys.stderr)
add_handler(ERROR, sys.stderr)
else: # pragma: no cover
pass
return logger
示例10: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def __init__(self):
logging.Filter.__init__(self)
self.modulesLevel = {}
示例11: test_filter
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def test_filter(self):
# Only messages satisfying the specified criteria pass through the
# filter.
filter_ = logging.Filter("spam.eggs")
handler = self.root_logger.handlers[0]
try:
handler.addFilter(filter_)
spam = logging.getLogger("spam")
spam_eggs = logging.getLogger("spam.eggs")
spam_eggs_fish = logging.getLogger("spam.eggs.fish")
spam_bakedbeans = logging.getLogger("spam.bakedbeans")
spam.info(self.next_message())
spam_eggs.info(self.next_message()) # Good.
spam_eggs_fish.info(self.next_message()) # Good.
spam_bakedbeans.info(self.next_message())
self.assert_log_lines([
('spam.eggs', 'INFO', '2'),
('spam.eggs.fish', 'INFO', '3'),
])
finally:
handler.removeFilter(filter_)
#
# First, we define our levels. There can be as many as you want - the only
# limitations are that they should be integers, the lowest should be > 0 and
# larger values mean less information being logged. If you need specific
# level values which do not fit into these limitations, you can use a
# mapping dictionary to convert between your application levels and the
# logging system.
#
示例12: test_logger_filter
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def test_logger_filter(self):
# Filter at logger level.
self.root_logger.setLevel(VERBOSE)
# Levels >= 'Verbose' are good.
self.log_at_all_levels(self.root_logger)
self.assert_log_lines([
('Verbose', '5'),
('Sociable', '6'),
('Effusive', '7'),
('Terse', '8'),
('Taciturn', '9'),
('Silent', '10'),
])
示例13: test_handler_filter
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def test_handler_filter(self):
# Filter at handler level.
self.root_logger.handlers[0].setLevel(SOCIABLE)
try:
# Levels >= 'Sociable' are good.
self.log_at_all_levels(self.root_logger)
self.assert_log_lines([
('Sociable', '6'),
('Effusive', '7'),
('Terse', '8'),
('Taciturn', '9'),
('Silent', '10'),
])
finally:
self.root_logger.handlers[0].setLevel(logging.NOTSET)
示例14: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def __init__(self, *errorfilter):
self.errorfilter = [logging.Filter(name) for name in errorfilter]
示例15: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import Filter [as 别名]
def __init__(self):
logging.Filter.__init__(self)
self.warning_table = {}