• json loggin 的使用,小案例


    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()
  • 相关阅读:
    js获取当前系统时间
    input标签之外是否一定添加form标签
    用css实现凹陷的线条
    js中的类型和函数参数传递类型问题
    jqDOM操作
    js中null与undefined的区别
    centos 支持复制与粘贴
    docker 部署springboot应用
    springboot springcloud 父项目pom工程创建pom文件
    自写流程图 支持批量展示
  • 原文地址:https://www.cnblogs.com/LMTlmt/p/11699931.html
Copyright © 2020-2023  润新知