get_logger now separates different log_levels

This commit is contained in:
gholt 2011-01-23 12:58:54 -08:00
parent cc638e7ed5
commit 9c3f4a17cf

View File

@ -396,41 +396,41 @@ def get_logger(conf, name=None, log_to_console=False, log_route=None):
:param name: Name of the logger :param name: Name of the logger
:param log_to_console: Add handler which writes to console on stderr :param log_to_console: Add handler which writes to console on stderr
""" """
if not conf:
conf = {}
if not hasattr(get_logger, 'root_logger_configured'): if not hasattr(get_logger, 'root_logger_configured'):
get_logger.root_logger_configured = True get_logger.root_logger_configured = True
get_logger(conf, name, log_to_console, log_route='root') get_logger(conf, name, log_to_console, log_route='root')
if name is None: if name is None:
name = conf.get('log_name', 'swift') if conf else 'swift' name = conf.get('log_name', 'swift')
if not log_route: if not log_route:
log_route = name log_route = name
if log_route == 'root': if log_route == 'root':
logger = logging.getLogger() logger = logging.getLogger()
else: else:
logger = logging.getLogger(log_route) logger = logging.getLogger(log_route)
if hasattr(get_logger, 'handler') and get_logger.handler: if not hasattr(get_logger, 'handlers'):
logger.removeHandler(get_logger.handler) get_logger.handlers = {}
get_logger.handler.close() facility = getattr(SysLogHandler, conf.get('log_facility', 'LOG_LOCAL0'),
get_logger.handler = None SysLogHandler.LOG_LOCAL0)
if facility in get_logger.handlers:
logger.removeHandler(get_logger.handlers[facility])
get_logger.handlers[facility].close()
del get_logger.handlers[facility]
if log_to_console: if log_to_console:
# check if a previous call to get_logger already added a console logger # check if a previous call to get_logger already added a console logger
if hasattr(get_logger, 'console') and get_logger.console: if hasattr(get_logger, 'console') and get_logger.console:
logger.removeHandler(get_logger.console) logger.removeHandler(get_logger.console)
get_logger.console = logging.StreamHandler(sys.__stderr__) get_logger.console = logging.StreamHandler(sys.__stderr__)
logger.addHandler(get_logger.console) logger.addHandler(get_logger.console)
if conf is None: get_logger.handlers[facility] = \
logger.setLevel(logging.INFO) SysLogHandler(address='/dev/log', facility=facility)
adapted_logger = LogAdapter(logger) logger.addHandler(get_logger.handlers[facility])
return adapted_logger
get_logger.handler = SysLogHandler(address='/dev/log',
facility=getattr(SysLogHandler,
conf.get('log_facility', 'LOG_LOCAL0'),
SysLogHandler.LOG_LOCAL0))
logger.addHandler(get_logger.handler)
logger.setLevel( logger.setLevel(
getattr(logging, conf.get('log_level', 'INFO').upper(), logging.INFO)) getattr(logging, conf.get('log_level', 'INFO').upper(), logging.INFO))
adapted_logger = LogAdapter(logger) adapted_logger = LogAdapter(logger)
formatter = NamedFormatter(name, adapted_logger) formatter = NamedFormatter(name, adapted_logger)
get_logger.handler.setFormatter(formatter) get_logger.handlers[facility].setFormatter(formatter)
if hasattr(get_logger, 'console'): if hasattr(get_logger, 'console'):
get_logger.console.setFormatter(formatter) get_logger.console.setFormatter(formatter)
return adapted_logger return adapted_logger