You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.5 KiB

4 years ago
  1. import sys
  2. from datetime import datetime
  3. from threading import Thread
  4. import Queue
  5. from boto.utils import RequestHook
  6. from boto.compat import long_type
  7. class RequestLogger(RequestHook):
  8. """
  9. This class implements a request logger that uses a single thread to
  10. write to a log file.
  11. """
  12. def __init__(self, filename='/tmp/request_log.csv'):
  13. self.request_log_file = open(filename, 'w')
  14. self.request_log_queue = Queue.Queue(100)
  15. Thread(target=self._request_log_worker).start()
  16. def handle_request_data(self, request, response, error=False):
  17. len = 0 if error else response.getheader('Content-Length')
  18. now = datetime.now()
  19. time = now.strftime('%Y-%m-%d %H:%M:%S')
  20. td = (now - request.start_time)
  21. duration = (td.microseconds + long_type(td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
  22. # write output including timestamp, status code, response time, response size, request action
  23. self.request_log_queue.put("'%s', '%s', '%s', '%s', '%s'\n" % (time, response.status, duration, len, request.params['Action']))
  24. def _request_log_worker(self):
  25. while True:
  26. try:
  27. item = self.request_log_queue.get(True)
  28. self.request_log_file.write(item)
  29. self.request_log_file.flush()
  30. self.request_log_queue.task_done()
  31. except:
  32. import traceback
  33. traceback.print_exc(file=sys.stdout)