import nnlog def write_log(content): pass log=nnlog.Logger('test.log',level='warn',backCount=5,when='S') #日志定义一次即可,不要放在循环中 #level:默认级别是debug,可以指定;backCount:,保留几天的日志,默认是5;when:创建日志的间隔,默认是一天产生一个D #日志的四个级别:由低到高,低级别可以打印高级别,高级别不会打印低级别。 log.debug('调试信息') log.info("正常打印的信息") log.warning('警告') log.error('调试信息') nnlog.Logger.surprise()
logging模块:
import logging LOG_NAME='%sutp.log'%time.strftime('%Y_%m_%d') LOG_PATH=os.path.join(BASE_PATH,'logs',LOG_NAME) logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) #只输入级别高于INFO的日志 handler = logging.FileHandler(LOG_PATH,encoding='UTF-8') handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) #使用: logger.info("Start print log") logger.debug("Do something") logger.warning("Something maybe fail.") logger.info("Finish")