LogViewTimeMiddleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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'
        }
    }
}

More like this

  1. Ignore Csrf Middleware by coleifer 3 years, 10 months ago
  2. Hours of Operation Middleware by andrew 5 years, 1 month ago
  3. GlobalRequest middleware by myq 1 year, 4 months ago
  4. middleware to capture doesnot exists exception by vbachu 9 months, 2 weeks ago
  5. NoDoubleSlashes middleware by simon 6 years, 3 months ago

Comments

(Forgotten your password?)