Fix duplicate logging

This commit is contained in:
gholt 2011-02-02 20:02:00 +00:00 committed by Tarmac
commit e7bba205b8
4 changed files with 21 additions and 12 deletions

View File

@ -1321,6 +1321,8 @@ class Swauth(object):
return False return False
def posthooklogger(self, env, req): def posthooklogger(self, env, req):
if not req.path.startswith(self.auth_prefix):
return
response = getattr(req, 'response', None) response = getattr(req, 'response', None)
if not response: if not response:
return return

View File

@ -409,28 +409,35 @@ def get_logger(conf, name=None, log_to_console=False, log_route=None):
logger = logging.getLogger() logger = logging.getLogger()
else: else:
logger = logging.getLogger(log_route) logger = logging.getLogger(log_route)
if not hasattr(get_logger, 'handlers'): logger.propagate = False
get_logger.handlers = {} if not hasattr(get_logger, 'handler4facility'):
get_logger.handler4facility = {}
facility = getattr(SysLogHandler, conf.get('log_facility', 'LOG_LOCAL0'), facility = getattr(SysLogHandler, conf.get('log_facility', 'LOG_LOCAL0'),
SysLogHandler.LOG_LOCAL0) SysLogHandler.LOG_LOCAL0)
if facility in get_logger.handlers: if facility in get_logger.handler4facility:
logger.removeHandler(get_logger.handlers[facility]) logger.removeHandler(get_logger.handler4facility[facility])
get_logger.handlers[facility].close() get_logger.handler4facility[facility].close()
del get_logger.handlers[facility] del get_logger.handler4facility[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)
get_logger.handlers[facility] = \ get_logger.handler4facility[facility] = \
SysLogHandler(address='/dev/log', facility=facility) SysLogHandler(address='/dev/log', facility=facility)
logger.addHandler(get_logger.handlers[facility]) if not hasattr(get_logger, 'handler4logger'):
get_logger.handler4logger = {}
if logger in get_logger.handler4logger:
logger.removeHandler(get_logger.handler4logger[logger])
get_logger.handler4logger[logger] = \
get_logger.handler4facility[facility]
logger.addHandler(get_logger.handler4facility[facility])
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.handlers[facility].setFormatter(formatter) get_logger.handler4facility[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

View File

@ -28,7 +28,7 @@ class MyDaemon(daemon.Daemon):
def __init__(self, conf): def __init__(self, conf):
self.conf = conf self.conf = conf
self.logger = utils.get_logger(None) self.logger = utils.get_logger(None, 'server')
MyDaemon.forever_called = False MyDaemon.forever_called = False
MyDaemon.once_called = False MyDaemon.once_called = False
@ -97,7 +97,7 @@ user = %s
# test user quit # test user quit
MyDaemon.run_forever = MyDaemon.run_quit MyDaemon.run_forever = MyDaemon.run_quit
sio = StringIO() sio = StringIO()
logger = logging.getLogger() logger = logging.getLogger('server')
logger.addHandler(logging.StreamHandler(sio)) logger.addHandler(logging.StreamHandler(sio))
logger = utils.get_logger(None, 'server') logger = utils.get_logger(None, 'server')
daemon.run_daemon(MyDaemon, conf_file, logger=logger) daemon.run_daemon(MyDaemon, conf_file, logger=logger)

View File

@ -287,7 +287,7 @@ Error: unable to locate %s
def test_get_logger(self): def test_get_logger(self):
sio = StringIO() sio = StringIO()
logger = logging.getLogger() logger = logging.getLogger('server')
logger.addHandler(logging.StreamHandler(sio)) logger.addHandler(logging.StreamHandler(sio))
logger = utils.get_logger(None, 'server') logger = utils.get_logger(None, 'server')
logger.warn('test1') logger.warn('test1')