python3输出指定log信息
问题背景:
win10
python xxx.py > c:test.txt
上面这句只能把信息输出到test.txt,但是控制台看不到信息
#######################################################################
要想在控制台和test.txt同时得到信息,如下操作:
官网源码:
import logging
logging.basicConfig(filename='log_examp.log',level=logging.DEBUG)#level=logging.DEBUG定义日志级别
logging.debug('This message should go to the log file')#输出级别level=logging.DEBUG
logging.info('So should this')#输出级别level=logging.INFO
logging.warning('And this, too')#输出级别level=logging.WARNING
实战项目部分代码:
1 def a(self): 2 #with open("info.csv","r") as csvfile: 3 with open("产品库模板(PLT导入)20161205_01.csv","r") as csvfile: 4 #读取csv文件,返回的是迭代类型 5 self.read = csv.reader(csvfile) 6 for self.i in self.read: 7 if self.i[8] =="" or self.i[8]=="9" or self.i[8]=='项目编号': 8 continue 9 else : 10 try: 11 self.b(self.i) 12 except Exception as e: 13 print(e) 14 self.logging.info(e) 15 16 finally: 17 print(self.i[8]) 18 self.logging.info(self.i[8])