初级应用
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s| %(filename)s[line:%(lineno)s] |%(levelname)s| %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=sys.path[0]+'/running_log.log',
filemode='a')
logging.info('This is log')
高级应用
logger = logging.getLogger()
logger.setLevel(logging.INFO)
consoleHand1er = logging.StreamHandler()
fileHandler = logging.FileHandler(filename = 'log_file.log')
formatter = logging.Formatter("%(asctime)s| %(filename)10s: %(lineno)4s |%(levelname)8s| %(message)s")
consoleHand1er.setFormatter(formatter)
fileHandler.setFormatter(formatter)
logger.addHandler(consoleHand1er)
logger.addHandler(fileHandler)
还可以设置log配置文件,这样项目运行时修改log输出级别就不需要停项目