mylog.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:SpiderConfig 作者: brady-chen 项目源码 文件源码
def deco_log(self, log_name, fun_name, check_error=False):
        """
        :param fun_name: ????????????
        :param log_name: ??????????????,????sys.argv[0][0:-3] + '.log'????py????log
        :param check_error: ???????????True?False????False
        :return:?????“????”???
        """
        # ??py?????????????????????
        self.init(log_name)
        # ??python2??nonlocal??????????????
        status = [1]
        msg = [1]
        if check_error:
            def log(func):
                def record(*args, **kwargs):
                    try:
                        t0 = time.time()
                        back = func(*args, **kwargs)
                        #??
                        run_time = time.time() - t0
                        #???????????info?????????
                        status[0] = 2
                        msg[0] = "%s?????????%s?" %(fun_name, run_time)
                        return back
                    # sys._getframe().f_code.co_name?????????
                    except IndexError, e:
                        status[0] = 3
                        msg[0] = "???:%s???????\n???:%s\n?????:\n%s" % (fun_name, e, traceback.format_exc())
                    except requests.ConnectionError, e:
                        status[0] = 4
                        msg[0] = "???:%s???????????DNS??????????\n???:%s\n?????:\n%s" % (fun_name, e, traceback.format_exc())
                    except requests.TooManyRedirects, e:
                        status[0] = 4
                        msg[0] = "???:%s????????????????\n???:%s\n?????:\n%s" % (fun_name, e, traceback.format_exc())
                    except requests.HTTPError, e:
                        status[0] = 4
                        msg[0] = "???:%s????200?????\n???:%s\n?????:\n%s" % (fun_name, e, traceback.format_exc())
                    except requests.RequestException, e:
                        status[0] = 4
                        msg[0] = "???:%s???requests????\n???:%s\n?????:\n%s" % (fun_name, e, traceback.format_exc())
                    except Exception, e:
                        status[0] = 5
                        msg[0] = "???:%s????????\n???:%s\n?????:\n%s" % (fun_name, e, traceback.format_exc())
                    finally:
                        self.fun_log_type(status[0], msg[0])
                return record
        else:
            def log(func):
                return func
        return log
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号