5个线程共享相同的记录器类实例和日志文件,并引发[Errno 24] Too many open files
尝试重写logging
方法以在每次创建记录后关闭file handler
。
class RootLogger():
def __init__(self):
self.logger = None
# skeleton for creating custom logger
# name - name of the logger, string or class/module name
def set_config(self, name, logfile):
self.logger = logging.getLogger(name)
# format of the message
formatter = logging.Formatter('%(asctime)s - [%(name)s] [%(levelname)s] %(message)s')
# log file rotation, 8mb
handler = RotatingFileHandler(logfile, maxBytes=8000000, backupCount=5)
handler.setFormatter(formatter)
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(handler)
self.logger.setLevel(logging.DEBUG)
self.logger.propagate = False
return self.logger
# patch for excluding 'too many opened files' error
def handler_close(self):
handlers = self.logger.handlers
for handler in handlers:
handler.close()
# override commands
def info(self, msg):
self.logger.info(msg)
self.handler_close()
def debug(self, msg):
self.logger.debug(msg)
self.handler_close()
def error(self, msg):
self.logger.error(msg)
self.handler_close()
def warning(self, msg):
self.logger.warning(msg)
self.handler_close()
def critical(self, msg):
self.logger.warning(msg)
self.handler_close()