Skip to content

Commit 4db5993

Browse files
committed
apply suggestions for registering log handlers
1 parent db99ca3 commit 4db5993

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

reframe/core/logging.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ def stream_handler_kind(handler):
412412
return logger
413413

414414

415+
# Registry for log handler creation functions
416+
_create_handlers = {}
417+
418+
419+
def register_log_handler(name):
420+
'''Register the decorated log handler creation function'''
421+
def _create_handler_wrapper(fn):
422+
_create_handlers[name] = fn
423+
return fn
424+
return _create_handler_wrapper
425+
426+
427+
@register_log_handler('file')
415428
def _create_file_handler(site_config, config_prefix):
416429
filename = os.path.expandvars(site_config.get(f'{config_prefix}/name'))
417430
if not filename:
@@ -431,6 +444,7 @@ def _create_file_handler(site_config, config_prefix):
431444
mode='a+' if append else 'w+')
432445

433446

447+
@register_log_handler('filelog')
434448
def _create_filelog_handler(site_config, config_prefix):
435449
basedir = os.path.abspath(os.path.join(
436450
site_config.get('systems/0/prefix'),
@@ -447,6 +461,7 @@ def _create_filelog_handler(site_config, config_prefix):
447461
ignore_keys=ignore_keys)
448462

449463

464+
@register_log_handler('syslog')
450465
def _create_syslog_handler(site_config, config_prefix):
451466
address = site_config.get(f'{config_prefix}/address')
452467

@@ -485,6 +500,7 @@ def _create_syslog_handler(site_config, config_prefix):
485500
return logging.handlers.SysLogHandler(address, facility_type, socket_type)
486501

487502

503+
@register_log_handler('stream')
488504
def _create_stream_handler(site_config, config_prefix):
489505
stream = site_config.get(f'{config_prefix}/name')
490506
if stream == 'stdout':
@@ -496,6 +512,7 @@ def _create_stream_handler(site_config, config_prefix):
496512
raise AssertionError(f'unknown stream: {stream}')
497513

498514

515+
@register_log_handler('graylog')
499516
def _create_graylog_handler(site_config, config_prefix):
500517
try:
501518
import pygelf
@@ -528,6 +545,7 @@ def _create_graylog_handler(site_config, config_prefix):
528545
json_default=jsonext.encode)
529546

530547

548+
@register_log_handler('httpjson')
531549
def _create_httpjson_handler(site_config, config_prefix):
532550
url = site_config.get(f'{config_prefix}/url')
533551
extras = site_config.get(f'{config_prefix}/extras')
@@ -678,20 +696,6 @@ def emit(self, record):
678696
raise LoggingError('logging failed') from e
679697

680698

681-
_create_handler_registry = {
682-
'file': _create_file_handler,
683-
'filelog': _create_filelog_handler,
684-
'syslog': _create_syslog_handler,
685-
'stream': _create_stream_handler,
686-
'graylog': _create_graylog_handler,
687-
'httpjson': _create_httpjson_handler,
688-
}
689-
690-
691-
def register_plugin_handler(create_plugin_handler):
692-
_create_handler_registry["plugin"] = create_plugin_handler
693-
694-
695699
def _extract_handlers(site_config, handlers_group):
696700
handler_prefix = f'logging/0/{handlers_group}'
697701
handlers_list = site_config.get(handler_prefix)
@@ -700,15 +704,15 @@ def _extract_handlers(site_config, handlers_group):
700704
handler_type = handler_config['type']
701705

702706
try:
703-
create_handler = _create_handler_registry[handler_type]
704-
hdlr = create_handler(site_config, f'{handler_prefix}/{i}')
705-
if hdlr is None:
706-
getlogger().warning('could not initialize the '
707-
f'{handler_type} handler; ignoring ...')
708-
continue
707+
create_handler = _create_handlers[handler_type]
709708
except KeyError:
710-
# Should not enter here
711-
raise AssertionError(f'unknown handler type: {handler_type}')
709+
raise ConfigError(f'unknown handler type: {handler_type}') from None
710+
711+
hdlr = create_handler(site_config, f'{handler_prefix}/{i}')
712+
if hdlr is None:
713+
getlogger().warning('could not initialize the '
714+
f'{handler_type} handler; ignoring ...')
715+
continue
712716

713717
level = site_config.get(f'{handler_prefix}/{i}/level')
714718
fmt = site_config.get(f'{handler_prefix}/{i}/format')

0 commit comments

Comments
 (0)