[Notes] [Git][BuildGrid/buildgrid][mablanch/23-new-logging] cli.py: Allow filtering log record on their domains



Title: GitLab

Martin Blanchard pushed to branch mablanch/23-new-logging at BuildGrid / buildgrid

Commits:

1 changed file:

Changes:

  • buildgrid/_app/cli.py
    ... ... @@ -139,6 +139,31 @@ class BuildGridCLI(click.MultiCommand):
    139 139
             return mod.cli
    
    140 140
     
    
    141 141
     
    
    142
    +class DebugFilter(logging.Filter):
    
    143
    +
    
    144
    +    def __init__(self, debug_domains, name=''):
    
    145
    +        super().__init__(name=name)
    
    146
    +        self.__domains_tree = {}
    
    147
    +
    
    148
    +        for domain in debug_domains.split(','):
    
    149
    +            domains_tree = self.__domains_tree
    
    150
    +            for label in domain.split('.'):
    
    151
    +                if label not in domains_tree:
    
    152
    +                    domains_tree[label] = {}
    
    153
    +                domains_tree = domains_tree[label]
    
    154
    +
    
    155
    +    def filter(self, record):
    
    156
    +        domains_tree = self.__domains_tree
    
    157
    +        for label in record.name.split('.'):
    
    158
    +            if '*' in domains_tree:
    
    159
    +                return True
    
    160
    +            if label not in domains_tree:
    
    161
    +                return False
    
    162
    +            domains_tree = domains_tree[label]
    
    163
    +
    
    164
    +        return True
    
    165
    +
    
    166
    +
    
    142 167
     @click.command(cls=BuildGridCLI, context_settings=CONTEXT_SETTINGS)
    
    143 168
     @click.option('--no-print', is_flag=True, show_default=True,
    
    144 169
                   help="Do not print log records to stdout/stderr.")
    
    ... ... @@ -157,10 +182,19 @@ def cli(context, no_print, verbose):
    157 182
     
    
    158 183
         # Do not register a stream handler if no-print is requested:
    
    159 184
         if not no_print:
    
    160
    -        logging.basicConfig(format=LOG_RECORD_FORMAT)
    
    185
    +        log_handler = logging.StreamHandler()
    
    186
    +
    
    187
    +        # Filter debug messages using BGD_MESSAGE_DEBUG value:
    
    188
    +        debug_domains = os.environ.get('BGD_MESSAGE_DEBUG', None)
    
    189
    +        if debug_domains:
    
    190
    +            log_handler.addFilter(DebugFilter(debug_domains))
    
    191
    +
    
    161 192
         else:
    
    162
    -        logging.basicConfig(format=LOG_RECORD_FORMAT,
    
    163
    -                            handlers=[logging.NullHandler()])
    
    193
    +        log_handler = logging.NullHandler()
    
    194
    +
    
    195
    +    logging.basicConfig(format=LOG_RECORD_FORMAT,
    
    196
    +                        handlers=[log_handler])
    
    197
    +
    
    164 198
         if verbose == 1:
    
    165 199
             logger.setLevel(logging.WARNING)
    
    166 200
         elif verbose == 2:
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]