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
This commit is contained in:
Florian Hines 2012-10-05 15:56:34 -05:00
parent a0d96b184d
commit 92826d0602
6 changed files with 34 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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