import json import os Base_path = os.path.join(os.path.abspath(".."),"龙茂天日志.log") print(Base_path) def Json_data(): if not os.path.isfile(Base_path): dic = {"username":'xxx',"password":1234} with open("longmaotian_dict_josn.txt",'w',encoding='utf-8') as f: json.dump(dic,f) f.close() else: with open("longmaotian_dict_josn.txt",'r',encoding="utf-8") as f1: dic = json.load(f1) username = dic.get("username") f1.close() return username def Logging(): import logging logger= logging.getLogger("报错日志") #全局级别 logger.setLevel(logging.DEBUG) # formatter = logging.Formatter("%(asctime)s - %(name)s - [%(lineno)d] - %(message)s")#时间,日志名字,报错行数,报错信息 formatter = logging.Formatter("%(asctime)s - %(name)s - [%(lineno)d] - %(levelname)s - %(message)s")#时间,日志名字,报错行数,报错的类型,报错信息 ch = logging.StreamHandler() #生成屏幕流 # 控制台 级别 ch.setLevel(logging.DEBUG) fh = logging.FileHandler(Base_path,"a+",encoding="utf-8") # 生成文件流 "文件名","方式",encoding="utf-8" #文件级别 fh.setLevel(logging.DEBUG) #把摸版给 ch,sh ch.setFormatter(formatter) fh.setFormatter(formatter) #更改日志级别 logger.setLevel(logging.INFO) #输出 屏幕,文件 logger.addHandler(ch) logger.addHandler(fh) return logger def buy_car(): logger = Logging() username = Json_data() if username == 'xxxx': print("登录成功") logger.error("error我来报一小个错%s" % "登录成功") logger.info("info我来报一小个错%s" % "登录成功") else: print("登录失败") logger.error("error我来报一小个错%s"%"登录失败") logger.info("info我来报一小个错%s" % "登录失败") buy_car()