python装饰器

方法上的装饰器:

https://eastlakeside.gitbooks.io/interpy-zh/content/decorators/deco_class.html#

装饰器给类扩充功能

http://python3-cookbook.readthedocs.io/zh_CN/latest/c09/p12_using_decorators_to_patch_class_definitions.html

def log_request_decorator(cls):
    # Get the original implementation
    # orig_do_log = cls.__do_log

    # Make a new definition
    def log_request(self, handler):
        from tornado.log import access_log
        if handler.get_status() < 400:
            log_method = access_log.info
        elif handler.get_status() < 500:
            log_method = access_log.warning
        else:
            log_method = access_log.error
        request_time = 1000.0 * handler.request.request_time()
        log_method("%d %s %s %.2fms \nHeaders:%s", handler.get_status(),
                   handler._request_summary(), handler.request.body, request_time, handler.request.headers)

    # Attach to the class and return
    cls.log_function = log_request
    return cls




@logutils.log_request_decorator
class BillingService(ServiceBase):
  def log_function(self, handler):
    do something
.......

  

转载于:https://www.cnblogs.com/peterpanzsy/p/6913693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值