一个带参数的装饰器

# -*- coding: utf-8 -*-
# @Time    : 2018/9/19 14:45
# @Author  : cxa
# @File    : decorator.py
# @Software: PyCharm

from common.logger import logger
import time
from functools import wraps
import traceback


def decorator(m):
    def _deco(func):
        @wraps(func)
        def timer(*args, **kwargs):
            try:
                start = time.time()
                func(*args, **kwargs)
                end = time.time()
                print(dir(func))
                logger.info(f"{m} file's {func.__name__} method is running,cost time{end-start}s")
            except Exception as e:
                logger.error(
                    f"{m} file's {func.__name__} method ,is error,there are details:{traceback.format_exc()}")

        return timer

    return _deco

posted @ 2018-09-19 15:15  公众号python学习开发  阅读(320)  评论(0编辑  收藏  举报