import logging import time # 获取字符串格式的当前日期 times = time.strftime("%Y%m%d",time.localtime()) # 获取logger对象,设置日志级别 logger = logging.getLogger() logger.setLevel(logging.DEBUG) # 创建FileHandler对象用于写入文件 handler1 = logging.FileHandler(filename="./log/%s_execution.log"%times, encoding='utf-8') # 设置文件日志级别 handler1.setLevel(logging.INFO) # 设置文件日志格式 fmt1 = logging.Formatter(fmt='%(levelname)s %(asctime)s %(filename)s line:[%(lineno)d:] %(message)s', datefmt='%Y-%m-%d %H:%M:%S') handler1.setFormatter(fmt1) # 创建StreamHandler对象用于输出到控制台 handler2 = logging.StreamHandler() # 设置控制台日志级别 handler2.setLevel(logging.INFO) # 设置控制台日志格式 fmt2 = logging.Formatter(fmt='%(filename)s line:[%(lineno)d:] %(message)s') handler2.setFormatter(fmt2) # 将handler1、handler2添加到logger logger.addHandler(handler1) logger.addHandler(handler2)
logging.info('测试‘)