Skip to content
Snippets Groups Projects
Commit b8325887 authored by Martin Blanchard's avatar Martin Blanchard
Browse files

cli.py: Allow filtering log record on their domains

This patch allows filtering the log records that get printed to stdout
and stderr based on their domain name using the BGD_MESSAGE_DEBUG
environment variable. A comma separated list of domains is expected:

  BGD_MESSAGE_DEBUG="buildgrid.server.cas,buildgrid.client.cas"
parent a2803612
No related branches found
No related tags found
No related merge requests found
......@@ -140,12 +140,38 @@ class BuildGridCLI(click.MultiCommand):
return mod.cli
class DebugFilter(logging.Filter):
def __init__(self, debug_domains, name=''):
super().__init__(name=name)
self.__domains_tree = {}
for domain in debug_domains.split(','):
domains_tree = self.__domains_tree
for label in domain.split('.'):
if label not in domains_tree:
domains_tree[label] = {}
domains_tree = domains_tree[label]
def filter(self, record):
domains_tree = self.__domains_tree
for label in record.name.split('.'):
if '*' in domains_tree:
return True
if label not in domains_tree:
return False
domains_tree = domains_tree[label]
return True
def setup_logging(verbosity=0, debug_mode=False):
"""Deals with loggers verbosity"""
asyncio_logger = logging.getLogger('asyncio')
root_logger = logging.getLogger()
log_handler = logging.StreamHandler(stream=sys.stdout)
for log_filter in root_logger.filters:
log_handler.addFilter(log_filter)
logging.basicConfig(format=LOG_RECORD_FORMAT, handlers=[log_handler])
......@@ -176,3 +202,8 @@ def cli(context):
root_logger.removeHandler(log_handler)
for log_filter in root_logger.filters[:]:
root_logger.removeFilter(log_filter)
# Filter debug messages using BGD_MESSAGE_DEBUG value:
debug_domains = os.environ.get('BGD_MESSAGE_DEBUG', None)
if debug_domains:
root_logger.addFilter(DebugFilter(debug_domains))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment