Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Add support to resolve child loggers. #1197

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/breadcrumbs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ the :py:func:`~raven.breadcrumbs.ignore_logger` and
with given arguments: `logger`, `level`, `msg`, `args` and `kwargs`
which are the values passed to the logging system. If the callback
returns true value the default handling is disabled. Only one callback
can be registered per one logger name. Logger tree is not traversed
so calling this method with `spammy_module` argument will not silence
messages from `spammy_module.child`.
can be registered per one logger name.

Typically it makes sense to invoke
:py:func:`~raven.breadcrumbs.record` from it unless you want to silence
Expand Down
20 changes: 17 additions & 3 deletions raven/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ def install_logging_hook():
_patch_logger()


def resolve_child_loggers(name):
"""Resolves children logger. Resolution will only work for loggers
that are registered before this function is called.
"""
children = []
for n in logging.Logger.manager.loggerDict.keys():
if n.startswith(name):
children.append(n)
return children


def ignore_logger(name_or_logger, allow_level=None):
"""Ignores a logger during breadcrumb recording.
"""
Expand All @@ -273,14 +284,17 @@ def register_special_log_handler(name_or_logger, callback):
with given arguments: `logger`, `level`, `msg`, `args` and `kwargs`
which are the values passed to the logging system. If the callback
returns true value the default handling is disabled. Only one callback
can be registered per one logger name. Logger tree is not traversed
so calling this method with `spammy_module` argument will not silence
messages from `spammy_module.child`.
can be registered per one logger name.
"""
if isinstance(name_or_logger, string_types):
name = name_or_logger
else:
name = name_or_logger.name

for c in resolve_child_loggers(name):
special_logger_handlers[c] = callback

# always add parent logger
special_logger_handlers[name] = callback


Expand Down
33 changes: 33 additions & 0 deletions tests/breadcrumbs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,39 @@ def handler(logger, level, msg, args, kwargs):
crumbs = client.context.breadcrumbs.get_buffer()
assert len(crumbs) == 0

# test child resolution
name = __name__
logger = logging.getLogger(name)

def handler(logger, level, msg, args, kwargs):
assert logger.name == name
assert msg == 'aha!'
return True

breadcrumbs.register_special_log_handler(name, handler)

client = Client('http://foo:[email protected]/0')
with client.context:
logger.debug('aha!')
crumbs = client.context.breadcrumbs.get_buffer()
assert len(crumbs) == 0

logger = logging.getLogger('superspecial')

# test positive case
def handler(logger, level, msg, args, kwargs):
assert logger.name == name
assert msg == 'aha!'
return True

breadcrumbs.register_special_log_handler(name, handler)

client = Client('http://foo:[email protected]/0')
with client.context:
logger.debug('aha!')
crumbs = client.context.breadcrumbs.get_buffer()
assert len(crumbs) == 1

def test_logging_handlers(self):
name = __name__ + '.superspecial2'
logger = logging.getLogger(name)
Expand Down