BF: safeguard closing of log handlers + close in reverse order

otherwise there might be "stuck" handler in the queue. and closing
exceptions can occur -- even stock logging guards in recent versions
pull/124/head^2
Yaroslav Halchenko 2013-02-21 20:58:27 -05:00
parent b36835f6f0
commit 012264dce1
1 changed files with 13 additions and 4 deletions

View File

@ -367,11 +367,20 @@ class Server:
logSys.error("Unable to log to " + target) logSys.error("Unable to log to " + target)
logSys.info("Logging to previous target " + self.__logTarget) logSys.info("Logging to previous target " + self.__logTarget)
return False return False
# Removes previous handlers # Removes previous handlers -- in reverse order since removeHandler
for handler in logging.getLogger("fail2ban").handlers: # alter the list in-place and that can confuses the iterable
# Closes the handler. for handler in logging.getLogger("fail2ban").handlers[::-1]:
# Remove the handler.
logging.getLogger("fail2ban").removeHandler(handler) logging.getLogger("fail2ban").removeHandler(handler)
# And try to close -- it might be closed already
try:
handler.flush()
handler.close() handler.close()
except ValueError:
if sys.version_info >= (2,6):
raise
# is known to be thrown after logging was shutdown once
# with older Pythons -- seems to be safe to ignore there
# tell the handler to use this format # tell the handler to use this format
hdlr.setFormatter(formatter) hdlr.setFormatter(formatter)
logging.getLogger("fail2ban").addHandler(hdlr) logging.getLogger("fail2ban").addHandler(hdlr)