在对象销毁的时候需要记得销毁句柄:
class NgLogger: def __init__(self, module, file_name): self.logger = logging.getLogger(module) self.logger.setLevel(logging.DEBUG) self._fh = logging.FileHandler(file_name) self._fh.setLevel(logging.DEBUG) # create console handler with a higher log level self._ch = logging.StreamHandler() self._ch.setLevel(logging.DEBUG) # create formatter and add it to the handlers formatter = logging.Formatter('%(levelname)s %(asctime)s [%(filename)s:%(lineno)d] %(message)s') self._fh.setFormatter(formatter) self._ch.setFormatter(formatter) # add the handlers to the logger self.logger.addHandler(self._fh) self.logger.addHandler(self._ch) def __del__(self): self.logger.info('write log finished') self.logger.removeHandler(self._fh) self.logger.removeHandler(self._ch)
不确认对象是否销毁可以del 对象
标签:ch,错位,读写,self,._,fh,logging,logger,日志 From: https://www.cnblogs.com/kid-kid/p/17862644.html