Python日志模块简单使用
def loginfo(info): # create logger logger = logging.getLogger('【cpu and mem】**********') # Set default log level logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.WARN) ch2 = logging.FileHandler(loginfofile) ch2.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s************* - %(message)s') #设置日志格式 ch.setFormatter(formatter) #添加到记录器 ch2.setFormatter(formatter) logger.addHandler(ch) #创建handler logger.addHandler(ch2) # 'application' code # logger.debug('debug message') logger.info('\n'+info) #写入需要记录的日志 logger.removeHandler(ch) #写完log后一定要删除hender,不然会造成handler存在多个,写入重复的问题 logger.removeHandler(ch2)