源碼:
def output(self, level, message): fh = logging.FileHandler(self.logpath, mode='a', encoding='utf-8') fh.setLevel(logging.DEBUG) fh.setFormatter(self.formatter) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(self.formatter) self.logger.addHandler(ch) self.logger.addHandler(fh)
結果:

文件無數據,控制台有數據
問題原因:
未將文件對象加到logger 中
修改后的代碼:
def output(self, level, message): fh = logging.FileHandler(self.logpath, mode='a', encoding='utf-8') fh.setLevel(logging.DEBUG) fh.setFormatter(self.formatter) self.logger.addHandler(fh) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(self.formatter) self.logger.addHandler(ch)