From 66e7a4cea37dffd9f9a7fb88c43ae63453bd4c50 Mon Sep 17 00:00:00 2001 From: Daniel Margala Date: Thu, 26 Sep 2024 09:09:29 -0700 Subject: [PATCH] Add support for plugin log handler --- reframe/core/logging.py | 44 +++++++++++++++++-------------------- reframe/schemas/config.json | 2 +- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/reframe/core/logging.py b/reframe/core/logging.py index d91b067fbe..997be8d4f0 100644 --- a/reframe/core/logging.py +++ b/reframe/core/logging.py @@ -678,39 +678,35 @@ def emit(self, record): raise LoggingError('logging failed') from e +_create_handler_registry = { + 'file': _create_file_handler, + 'filelog': _create_filelog_handler, + 'syslog': _create_syslog_handler, + 'stream': _create_stream_handler, + 'graylog': _create_graylog_handler, + 'httpjson': _create_httpjson_handler, +} + + +def register_plugin_handler(create_plugin_handler): + _create_handler_registry["plugin"] = create_plugin_handler + + def _extract_handlers(site_config, handlers_group): handler_prefix = f'logging/0/{handlers_group}' handlers_list = site_config.get(handler_prefix) handlers = [] for i, handler_config in enumerate(handlers_list): handler_type = handler_config['type'] - if handler_type == 'file': - hdlr = _create_file_handler(site_config, f'{handler_prefix}/{i}') - elif handler_type == 'filelog': - hdlr = _create_filelog_handler( - site_config, f'{handler_prefix}/{i}' - ) - elif handler_type == 'syslog': - hdlr = _create_syslog_handler(site_config, f'{handler_prefix}/{i}') - elif handler_type == 'stream': - hdlr = _create_stream_handler(site_config, f'{handler_prefix}/{i}') - elif handler_type == 'graylog': - hdlr = _create_graylog_handler( - site_config, f'{handler_prefix}/{i}' - ) - if hdlr is None: - getlogger().warning('could not initialize the ' - 'graylog handler; ignoring ...') - continue - elif handler_type == 'httpjson': - hdlr = _create_httpjson_handler( - site_config, f'{handler_prefix}/{i}' - ) + + try: + create_handler = _create_handler_registry[handler_type] + hdlr = create_handler(site_config, f'{handler_prefix}/{i}') if hdlr is None: getlogger().warning('could not initialize the ' - 'httpjson handler; ignoring ...') + f'{handler_type} handler; ignoring ...') continue - else: + except KeyError: # Should not enter here raise AssertionError(f'unknown handler type: {handler_type}') diff --git a/reframe/schemas/config.json b/reframe/schemas/config.json index a3b9eccd1a..7bee4836f9 100644 --- a/reframe/schemas/config.json +++ b/reframe/schemas/config.json @@ -51,7 +51,7 @@ "properties": { "type": { "type": "string", - "enum": ["file", "filelog", "graylog", "stream", "syslog", "httpjson"] + "enum": ["file", "filelog", "graylog", "stream", "syslog", "httpjson", "plugin"] }, "level": {"$ref": "#/defs/loglevel"}, "format": {"type": "string"},