代码如下,对照了下,没找到问题!!!!!
import logging
from logging.handlers import TimedRotatingFileHandler
log_path = ""
class Log():
def __init__(self, logname, logger,loglevel=logging.DEBUG):
# set log level
self.logger = logging.getLogger(logger)
self.logger.setLevel(loglevel)
# file handler for log
#fh = logging.FileHandler(logname)
fh = TimedRotatingFileHandler(log_path+logname, when='D' ,interval=10,backupCount=40)
fh.setLevel(loglevel)
# stream handler for log
ch = logging.StreamHandler()
ch.setLevel(loglevel)
# define log format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(funcName)s - %(levelname)s - %(message)s')
#formatter = format_dict[int(loglevel)]
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add logger handler
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def getlog(self):
return self.logger
logger = Log(logname='log.txt', logger="fb_api").getlog()
if __name__=='__main__':
log_test = Log('test.log', logger='test').getlog()
log_test.info('info')
log_test.debug('debug')
log_test.error('error')
出现这个问题的原因是在不同的地方调用Logger的时候,logname传的实参都是一样的
我用我的电脑 运行你的代码没有出现你说的问题。
是不是你还运行了其他东西
同上,我的环境上也没出现楼主说的情况