1class CountRequestsMiddleware:
2
3 def __init__(self, get_response):
4 self.get_response = get_response
5 self.count_requests = 0
6 self.count_exceptions = 0
7
8 def __call__(self, request, *args, **kwargs):
9 self.count_requests += 1
10 logger.info(f"Handled {self.count_requests} requests so far")
11 return self.get_response(request)
12
13 def process_exception(self, request, exception):
14 self.count_exceptions += 1
15 logger.error(f"Encountered {self.count_exceptions} exceptions so far")
16