在logging中,Logger's level 的默认等级为warning
所以虽然在handler中setlervel了,Logger's level 和Handler's Level 但是level取较高的那个(待校验)
所以日志的level 为warning
解决此问题可以采用
logging.root.setLevel(logging.NOTSET)
完整源码如下图:
import logging
class loggerr(object):
def __init__(self,logger):
self.logger = logging.getLogger(logger)
self.fmt=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(message)s ')
logging.root.setLevel(logging.NOTSET)
#logging.basicConfig(level=logging.NOTSET)
def getlog(self):
ch=logging.StreamHandler()
sh=logging.FileHandler('log1.log')
ch.setFormatter(self.fmt)
sh.setFormatter(self.fmt)
ch.setLevel(logging.INFO)
sh.setLevel(logging.INFO)
self.logger.addHandler(ch)
self.logger.addHandler(sh)
return self.logger
if __name__ == '__main__':
mylog=loggerr('mylog').getlog()
mylog.info('infolog')
mylog.debug('debuglog')
mylog.warning('warninglog')
mylog.error('errorlog')