用python编写一个更好看好用的日志库

相信现在很多做自动化测试、开发,一般用的都是python的logging来记录日志,但是,logging确实不是很好看,只有一个红色的,在控制台中也不好分辨,那能不能自己写一个好看点的呢?我已经写好一个了,需要的可以直接下载安装试试,下面来说下我是怎么思考怎么写的。

pip install luckylog

 首先,需要知道的知识点储备:

1、sys._getframe的使用

2、装饰器的编写

3、使用'\033[ \033[0m'来设置字体颜色

4、threading递归锁

好了,以上就是我们需要的额外知识点了,现在我们开始写吧。

首先,我想要定义四种方式的日志,success、erro、warning、tip,success返回是绿色,erro返回是红色,warning是黄色、tip是默认或者蓝色,前期我们只要直接能打印,然后能够放入文件中,开始第一步,定义几种类型的消息返回形式:

class LogConfig():
    __num = 2
    def conf_success(self,*args,**kwargs) -> str:
        now_time = time.strftime('%Y-%m-%d %H:%M:%S')
        def_name = sys._getframe(self.__num).f_code.co_name
        line = sys._getframe(self.__num).f_lineno
        file_path = sys._getframe(self.__num).f_code.co_filename

        return '(p≧w≦q) === {} FILE_PATH=({}) FUNC-({}) <SUCCESS> LINE-{} *MSG==√{} ==='.format(now_time, file_path,def_name, line, args,kwargs)
    def conf_erro(self,*args,**kwargs) -> str:
        now_time = time.strftime('%Y-%m-%d %H:%M:%S')
        def_name = sys._getframe(self.__num).f_code.co_name
        line = sys._getframe(self.__num).f_lineno
        file_path = sys._getframe(self.__num).f_code.co_filename
        return 'φ(゜▽゜*)♪ === {} FILE_PATH=({}) FUNC-({}) <ERRO> LINE-{} *MSG==×{} ==='.format(now_time,file_path,def_name,line,args,kwargs)
    def conf_warning(self,*args,**kwargs) -> str:
        now_time = time.strftime('%Y-%m-%d %H:%M:%S')
        def_name = sys._getframe(self.__num).f_code.co_name
        line = sys._getframe(self.__num).f_lineno
        file_path = sys._getframe(self.__num).f_code.co_filename
        return 'o(* ̄▽ ̄*)ブ === {} FILE_PATH=({}) FUNC-({}) <WORNING> LINE-{} *MSG==!{} ==='.format(now_time,file_path,def_name, line, args,kwargs)
    def conf_normal(self,*args,**kwargs) -> str:
        now_time = time.strftime('%Y-%m-%d %H:%M:%S')
        def_name = sys._getframe(2).f_code.co_name
        line = sys._getframe(2).f_lineno
        file_path = sys._getframe(self.__num).f_code.co_filename
        return 'm( ̄︶ ̄)↗ === {} FILE_PATH=({}) FUNC-({}) <TIP> LINE-{} *MSG=={} ==='.format(now_time,file_path,def_name, line, args,kwargs)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值