1 import logging 2 3 #创建logger对象 4 loggers = logging.getLogger() 5 6 #创建文件输输入对象hander 7 wj = logging.FileHandler("ww.log") 8 # kzhit = logging.StreamHandler() #创建控制台hander 9 #创建格式 10 formatter = logging.Formatter("%(asctime)s-%(name)s-%(levelname)s-%(message)s-%(pathname)s ") 11 12 #添加格式到文件中 13 wj.setFormatter(formatter) 14 15 #添加到控制台中 16 # kzhit.setFormatter(formatter) 17 18 #logger对象中添加对象(文件输入对象) 19 loggers.addHandler(wj) 20 # loggers.addHandler(kzhit) 21 22 loggers.setLevel(logging.DEBUG) #bug级别 23 24 logging.debug("debug") 25 logging.info("info") 26 logging.error("error") 27 logging.warning("warning") 28 logging.critical("critical")