Python3基础-错误集锦
1、TypeError: Level not an integer or a valid string: <function info at 0x03559348>
#源代码 """ import logging logging.basicConfig(filename='log.log', format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', level=logging.info, datefmt='%a, %d %b %Y %H:%M:%S', filemode='w' ) logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
将 level=logging.info修改为 level=logging.INFO;则写入log.log文件执行成功
Fri, 01 Nov 2019 09:54:17 logging_test.py[line:17] INFO info message Fri, 01 Nov 2019 09:54:17 logging_test.py[line:18] WARNING warning message Fri, 01 Nov 2019 09:54:17 logging_test.py[line:19] ERROR error message Fri, 01 Nov 2019 09:54:17 logging_test.py[line:20] CRITICAL critical message
总结:设置level时候 要将级别全部设置为大写 logging.DEBUG、logging.INFO、logging.WARNING、logging.ERROR、logging.CRITICAL
2、文件日志级别 设置无效的问题
import logging logger = logging.getLogger('loggertest') #创建一个logger对象 # 建立一个filehandler来把日志记录在文件里,级别为debug以上 fh = logging.FileHandler('test.log','w') fh.setLevel(logging.DEBUG) # 建立一个streamhandler来把日志打在CMD窗口上,级别为error以上 ch = logging.StreamHandler() ch.setLevel(logging.ERROR) #设置日志格式 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) #将想要欸地handler添加到logger对象中 logger.addHandler(fh) logger.addHandler(ch) #开始打印日志 logger.debug('debug message') logger.info('info message') logger.warning('warning message') logger.error('error message') logger.critical('critical message')
文件输出的日志为
2019-11-01 11:44:10,584 - loggertest - WARNING - warning message 2019-11-01 11:44:10,584 - loggertest - ERROR - error message 2019-11-01 11:44:10,585 - loggertest - CRITICAL - critical message
因为未设置logger = logging.getLogger('loggertest')的级别,则默认级别为WARNING; 设置文件输出日志级别为DEBUG,则WARNING>DEBUG;则只能输出WARNING级别以上的日志
logger = logging.getLogger('loggertest') #创建一个logger对象
logger.setLevel(logging.DEBUG) #新增该代码
则再次执行,文件输出的日志为
2019-11-01 11:46:39,413 - loggertest - DEBUG - debug message 2019-11-01 11:46:39,413 - loggertest - INFO - info message 2019-11-01 11:46:39,413 - loggertest - WARNING - warning message 2019-11-01 11:46:39,414 - loggertest - ERROR - error message 2019-11-01 11:46:39,414 - loggertest - CRITICAL - critical message
3、UnboundLocalError: local variable 'country' referenced before assignment
错误代码如下:
country = 'china' class student: def __init__(self,name,country): self.name = name def play_ball(self): print("未修改的:",country) #可输出的是china country = 1 #print("修改后的:", country) p1=student('susu','china') p1.play_ball()
python中出现了没有声明的变量 , py是通过如下简单的规则找出变量的范围 :如果函数内部有对变量的赋值 ,则该变量被认为是本地的,此时可以正常修改。但是若变量不定义在函数内部,且没有进行变量范围的声明(去调用外部变量),程序在函数内部找不到相应变量,所以会出现未定义变量的错误提示消息
country = 'china' class student: def __init__(self,name): self.name = name def play_ball(self): global country print("未修改的:",country) #可输出的是china country = 1 print("修改后的:", country) p1=student('susu') p1.play_ball() print(country) """ 执行结果如下: 未修改的: china 修改后的: 1 1 """