方法上的裝飾器:html
https://eastlakeside.gitbooks.io/interpy-zh/content/decorators/deco_class.html#python
裝飾器給類擴充功能git
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
.......