From 92826d0602d8eb8956f5244e9dfdf8594ce0eefe Mon Sep 17 00:00:00 2001 From: Florian Hines Date: Fri, 5 Oct 2012 15:56:34 -0500 Subject: [PATCH] add support for custom log handlers Add a hook to get_logger to run custom functions to add custom log handlers or the like. Change-Id: Ib04b12939dcac7e4ad6453dea9795682044c6ae0 --- etc/account-server.conf-sample | 4 ++++ etc/container-server.conf-sample | 4 ++++ etc/object-expirer.conf-sample | 4 ++++ etc/object-server.conf-sample | 4 ++++ etc/proxy-server.conf-sample | 4 ++++ swift/common/utils.py | 14 ++++++++++++++ 6 files changed, 34 insertions(+) diff --git a/etc/account-server.conf-sample b/etc/account-server.conf-sample index 3e5ee175bb..d037057c75 100644 --- a/etc/account-server.conf-sample +++ b/etc/account-server.conf-sample @@ -13,6 +13,10 @@ # log_facility = LOG_LOCAL0 # log_level = INFO # log_address = /dev/log +# comma separated list of functions to call to setup custom log handlers. +# functions get passed: conf, name, log_to_console, log_route, fmt, logger, +# adapted_logger +# log_custom_handlers = # If set, log_udp_host will override log_address # log_udp_host = # log_udp_port = 514 diff --git a/etc/container-server.conf-sample b/etc/container-server.conf-sample index 0f55a863cd..e78123adee 100644 --- a/etc/container-server.conf-sample +++ b/etc/container-server.conf-sample @@ -16,6 +16,10 @@ # log_facility = LOG_LOCAL0 # log_level = INFO # log_address = /dev/log +# comma separated list of functions to call to setup custom log handlers. +# functions get passed: conf, name, log_to_console, log_route, fmt, logger, +# adapted_logger +# log_custom_handlers = # If set, log_udp_host will override log_address # log_udp_host = # log_udp_port = 514 diff --git a/etc/object-expirer.conf-sample b/etc/object-expirer.conf-sample index ac8bf4f96c..cef0f0f19d 100644 --- a/etc/object-expirer.conf-sample +++ b/etc/object-expirer.conf-sample @@ -6,6 +6,10 @@ # log_facility = LOG_LOCAL0 # log_level = INFO # log_address = /dev/log +# comma separated list of functions to call to setup custom log handlers. +# functions get passed: conf, name, log_to_console, log_route, fmt, logger, +# adapted_logger +# log_custom_handlers = # If set, log_udp_host will override log_address # log_udp_host = # log_udp_port = 514 diff --git a/etc/object-server.conf-sample b/etc/object-server.conf-sample index 0ef979e875..6ffd2b13ce 100644 --- a/etc/object-server.conf-sample +++ b/etc/object-server.conf-sample @@ -14,6 +14,10 @@ # log_facility = LOG_LOCAL0 # log_level = INFO # log_address = /dev/log +# comma separated list of functions to call to setup custom log handlers. +# functions get passed: conf, name, log_to_console, log_route, fmt, logger, +# adapted_logger +# log_custom_handlers = # If set, log_udp_host will override log_address # log_udp_host = # log_udp_port = 514 diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index 4a507b6f36..a28cac733f 100644 --- a/etc/proxy-server.conf-sample +++ b/etc/proxy-server.conf-sample @@ -15,6 +15,10 @@ # log_level = INFO # log_headers = False # log_address = /dev/log +# comma separated list of functions to call to setup custom log handlers. +# functions get passed: conf, name, log_to_console, log_route, fmt, logger, +# adapted_logger +# log_custom_handlers = # If set, log_udp_host will override log_address # log_udp_host = # log_udp_port = 514 diff --git a/swift/common/utils.py b/swift/common/utils.py index f2bee5cb55..abda13defa 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -687,6 +687,20 @@ def get_logger(conf, name=None, log_to_console=False, log_route=None, logger.statsd_client = None adapted_logger = LogAdapter(logger, name) + other_handlers = conf.get('log_custom_handlers', None) + if other_handlers: + log_custom_handlers = [s.strip() for s in other_handlers.split(',') + if s.strip()] + for hook in log_custom_handlers: + try: + mod, fnc = hook.rsplit('.', 1) + logger_hook = getattr(__import__(mod, fromlist=[fnc]), fnc) + logger_hook(conf, name, log_to_console, log_route, fmt, + logger, adapted_logger) + except (AttributeError, ImportError): + print >>sys.stderr, 'Error calling custom handler [%s]' % hook + except ValueError: + print >>sys.stderr, 'Invalid custom handler format [%s]' % hook return adapted_logger