Skip to content
Snippets Groups Projects
Commit 97023a78 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 a09a386d
No related branches found
No related tags found
Loading
...@@ -139,6 +139,31 @@ class BuildGridCLI(click.MultiCommand): ...@@ -139,6 +139,31 @@ class BuildGridCLI(click.MultiCommand):
return mod.cli 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
@click.command(cls=BuildGridCLI, context_settings=CONTEXT_SETTINGS) @click.command(cls=BuildGridCLI, context_settings=CONTEXT_SETTINGS)
@click.option('--no-print', is_flag=True, show_default=True, @click.option('--no-print', is_flag=True, show_default=True,
help="Do not print log records to stdout/stderr.") help="Do not print log records to stdout/stderr.")
...@@ -157,10 +182,19 @@ def cli(context, no_print, verbose): ...@@ -157,10 +182,19 @@ def cli(context, no_print, verbose):
# Do not register a stream handler if no-print is requested: # Do not register a stream handler if no-print is requested:
if not no_print: if not no_print:
logging.basicConfig(format=LOG_RECORD_FORMAT) log_handler = logging.StreamHandler()
# Filter debug messages using BGD_MESSAGE_DEBUG value:
debug_domains = os.environ.get('BGD_MESSAGE_DEBUG', None)
if debug_domains:
log_handler.addFilter(DebugFilter(debug_domains))
else: else:
logging.basicConfig(format=LOG_RECORD_FORMAT, log_handler = logging.NullHandler()
handlers=[logging.NullHandler()])
logging.basicConfig(format=LOG_RECORD_FORMAT,
handlers=[log_handler])
if verbose == 1: if verbose == 1:
logger.setLevel(logging.WARNING) logger.setLevel(logging.WARNING)
elif verbose == 2: elif verbose == 2:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment