不知道怎么回事,一个日志本地Macbook没问题,线上centos就输出了一个空文件:
原来代码
logHandler = TimedRotatingFileHandler(log_file, when='midnight', interval=1) logHandler.setLevel(level=logging.INFO) logging.basicConfig(level=logging.INFO, format='%(asctime)s %(pathname)s %(filename)s %(funcName)s %(lineno)d %(levelname)s - %(message)s', handlers=[logHandler]) print("__name__ is " + str(__name__)) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO)
这里用了basicConfig,结果logging file是个空文件
改成下面,去掉basicConfig就好了,原因未知
logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) log_format = logging.Formatter('%(asctime)s %(pathname)s %(filename)s %(funcName)s %(lineno)d %(levelname)s - %(message)s') logHandler = TimedRotatingFileHandler(log_file, when='midnight', interval=1) logHandler.setLevel(level=logging.INFO) logHandler.setFormatter(log_format) logger.addHandler(logHandler)