monkey_patches.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. __package__ = 'archivebox'
  2. import django_stubs_ext
  3. django_stubs_ext.monkeypatch()
  4. # monkey patch django timezone to add back utc (it was removed in Django 5.0)
  5. import datetime
  6. from django.utils import timezone
  7. timezone.utc = datetime.timezone.utc
  8. # monkey patch django-signals-webhooks to change how it shows up in Admin UI
  9. # from signal_webhooks.apps import DjangoSignalWebhooksConfig
  10. # DjangoSignalWebhooksConfig.verbose_name = 'API'
  11. # Install rich for pretty tracebacks in console logs
  12. # https://rich.readthedocs.io/en/stable/traceback.html#traceback-handler
  13. from rich.traceback import install
  14. install(show_locals=True)
  15. from daphne import access
  16. class ModifiedAccessLogGenerator(access.AccessLogGenerator):
  17. """Clutge workaround until daphne uses the Python logging framework. https://github.com/django/daphne/pull/473/files"""
  18. def write_entry(self, host, date, request, status=None, length=None, ident=None, user=None):
  19. # Ignore noisy requests to staticfiles / favicons / etc.
  20. if 'GET /static/' in request:
  21. return
  22. if "GET /health/" in request:
  23. return
  24. if 'GET /admin/jsi18n/' in request:
  25. return
  26. if request.endswith("/favicon.ico") or request.endswith("/robots.txt") or request.endswith("/screenshot.png"):
  27. return
  28. if request.endswith('.css') or request.endswith('.js') or request.endswith('.woff') or request.endswith('.ttf'):
  29. return
  30. if str(status) in ('404', '304'):
  31. return
  32. # clean up the log format to mostly match the same format as django.conf.settings.LOGGING rich formats
  33. self.stream.write(
  34. "[%s] HTTP %s (%s) %s\n"
  35. % (
  36. date.strftime("%Y-%m-%d %H:%M:%S"),
  37. request,
  38. status or "-",
  39. "localhost" if host.startswith("127.") else host.split(":")[0],
  40. )
  41. )
  42. access.AccessLogGenerator.write_entry = ModifiedAccessLogGenerator.write_entry