我不明白为什么我的自定义记录器在多个线程中使用时也会导致OSError [Errno 24] Too many open files
。
我已经重新发明了一个轮子,每次写入文件后都会关闭文件处理器。
在我的代码中,我为每个记录器记录使用threading.RLock()
。
class RootLogger():
def __init__(self):
self.logger = None
def set_config(self, name, logfile):
self.logger = logging.getLogger(name)
formatter = logging.Formatter('%(asctime)s - [%(name)s] [%(levelname)s] %(message)s',
"%Y-%m-%d %H:%M:%S")
handler = logging.FileHandler(logfile, mode='a')
handler.setFormatter(formatter)
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(handler)
self.logger.setLevel(logging.DEBUG)
return self
def handler_close(self):
self.logger.handlers[0].close()
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()