class LogViewTimeMiddleware(object): LOG_MSG = '{0}\t{1}' def process_view(self, request, view_func, view_args, view_kwargs): request.start_time = datetime.datetime.now() def process_response(self, request, response): msg = self._GetResponseTimeMsg(request) logger = logging.getLogger('view_time') logger.info(msg) return response def _GetResponseTimeMsg(self, request): msg = None if hasattr(request, 'start_time'): end_time = datetime.datetime.now() start_time = request.start_time delta_time = end_time - start_time request_path = request.get_full_path() msg = self.LOG_MSG.format(request_path, delta_time) return msg ####################### # settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'date_tab_msg':{ 'format': '%(asctime)s\t%(message)s' } }, 'handlers': { 'view_time':{ 'level':'INFO', 'class':'logging.handlers.RotatingFileHandler', 'formatter':'date_tab_msg', 'filename':'view.time.log', 'maxBytes':10**9, 'backupCount':2 } }, 'loggers': { 'view_time':{ 'handlers':['view_time'], 'level': 'INFO' } } }