From 8c1b5677857e0b09e958d3deff3700dacaf6a2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 28 Jan 2022 08:01:21 +0100 Subject: Add extra information to logger. --- main.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 9adcee4..b88132f 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,17 @@ import logging import logging.config +class PackageFilter(logging.Filter): + + def __init__(self): + self.package = False + + def filter(self, record): + if self.package: + record.package = self.package + return True + + def get_config_files(): subpath = ['aur-runner', 'config.yaml'] conf_files = [] @@ -82,6 +93,8 @@ def get_conf(key, default=None): logging.config.dictConfig(get_conf('logging')) logger = logging.getLogger(__name__) +filter = PackageFilter() +logger.addFilter(filter) log_levels = { @@ -96,7 +109,7 @@ log_levels = { } -def subprocess_with_log(*args, capture_output=False, **kv_args): +def subprocess_with_log(args, capture_output=False, **kv_args): """ Run a process as by subprocess.run, but also log output which isn't captured by user. @@ -114,7 +127,15 @@ def subprocess_with_log(*args, capture_output=False, **kv_args): kv_args['stderr'] = subprocess.PIPE get_port = lambda process: process.stderr - process = subprocess.Popen(*args, **kv_args,) + if type(args) == list: + process_name = args[0] + elif type(args) == str: + process_name = args.split(' ')[0] + else: + process_name = 'Unknown' + + + process = subprocess.Popen(args, **kv_args,) logger.info('pid = %i, exec(%s)', process.pid, process.args) if kv_args.get('text'): @@ -135,7 +156,10 @@ def subprocess_with_log(*args, capture_output=False, **kv_args): except UnicodeDecodeError: pass - logging.log(level, '%i: %s', process.pid, line) + logger.log(level, '%s', line, extra={ + 'pid':process.pid, + 'process_name': process_name, + }) process.wait() @@ -246,6 +270,7 @@ def main(): failed_packages = [] for package in aur_pkgs: + filter.package = package cmd = subprocess_with_log(['auracle', *auracle_args, '--chdir', cachedir, 'clone', package], capture_output=True, text=True) m = re.match('[^:]*: (.*)', cmd.stdout) @@ -262,6 +287,8 @@ def main(): if cmd.returncode != 0: failed_packages.append(package) + filter.package = False + if failed_packages: logger.warning('The following packages failed: %s', failed_packages) -- cgit v1.2.3