log.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import logging
  2. import sys
  3. import warnings
  4. from django.conf import settings
  5. from django.core import mail
  6. from django.core.mail import get_connection
  7. from django.utils.deprecation import RemovedInNextVersionWarning
  8. from django.utils.module_loading import import_string
  9. from django.views.debug import ExceptionReporter, get_exception_reporter_filter
  10. # Imports kept for backwards-compatibility in Django 1.7.
  11. from logging import NullHandler # NOQA
  12. from logging.config import dictConfig # NOQA
  13. getLogger = logging.getLogger
  14. # Default logging for Django. This sends an email to the site admins on every
  15. # HTTP 500 error. Depending on DEBUG, all other log records are either sent to
  16. # the console (DEBUG=True) or discarded by mean of the NullHandler (DEBUG=False).
  17. DEFAULT_LOGGING = {
  18. 'version': 1,
  19. 'disable_existing_loggers': False,
  20. 'filters': {
  21. 'require_debug_false': {
  22. '()': 'django.utils.log.RequireDebugFalse',
  23. },
  24. 'require_debug_true': {
  25. '()': 'django.utils.log.RequireDebugTrue',
  26. },
  27. },
  28. 'handlers': {
  29. 'console': {
  30. 'level': 'INFO',
  31. 'filters': ['require_debug_true'],
  32. 'class': 'logging.StreamHandler',
  33. },
  34. 'null': {
  35. 'class': 'logging.NullHandler',
  36. },
  37. 'mail_admins': {
  38. 'level': 'ERROR',
  39. 'filters': ['require_debug_false'],
  40. 'class': 'django.utils.log.AdminEmailHandler'
  41. }
  42. },
  43. 'loggers': {
  44. 'django': {
  45. 'handlers': ['console'],
  46. },
  47. 'django.request': {
  48. 'handlers': ['mail_admins'],
  49. 'level': 'ERROR',
  50. 'propagate': False,
  51. },
  52. 'django.security': {
  53. 'handlers': ['mail_admins'],
  54. 'level': 'ERROR',
  55. 'propagate': False,
  56. },
  57. 'py.warnings': {
  58. 'handlers': ['console'],
  59. },
  60. }
  61. }
  62. def configure_logging(logging_config, logging_settings):
  63. if not sys.warnoptions:
  64. # Route warnings through python logging
  65. logging.captureWarnings(True)
  66. # RemovedInNextVersionWarning is a subclass of DeprecationWarning which
  67. # is hidden by default, hence we force the "default" behavior
  68. warnings.simplefilter("default", RemovedInNextVersionWarning)
  69. if logging_config:
  70. # First find the logging configuration function ...
  71. logging_config_func = import_string(logging_config)
  72. logging_config_func(DEFAULT_LOGGING)
  73. # ... then invoke it with the logging settings
  74. if logging_settings:
  75. logging_config_func(logging_settings)
  76. class AdminEmailHandler(logging.Handler):
  77. """An exception log handler that emails log entries to site admins.
  78. If the request is passed as the first argument to the log record,
  79. request data will be provided in the email report.
  80. """
  81. def __init__(self, include_html=False, email_backend=None):
  82. logging.Handler.__init__(self)
  83. self.include_html = include_html
  84. self.email_backend = email_backend
  85. def emit(self, record):
  86. try:
  87. request = record.request
  88. subject = '%s (%s IP): %s' % (
  89. record.levelname,
  90. ('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
  91. else 'EXTERNAL'),
  92. record.getMessage()
  93. )
  94. filter = get_exception_reporter_filter(request)
  95. request_repr = '\n{0}'.format(filter.get_request_repr(request))
  96. except Exception:
  97. subject = '%s: %s' % (
  98. record.levelname,
  99. record.getMessage()
  100. )
  101. request = None
  102. request_repr = "unavailable"
  103. subject = self.format_subject(subject)
  104. if record.exc_info:
  105. exc_info = record.exc_info
  106. else:
  107. exc_info = (None, record.getMessage(), None)
  108. message = "%s\n\nRequest repr(): %s" % (self.format(record), request_repr)
  109. reporter = ExceptionReporter(request, is_email=True, *exc_info)
  110. html_message = reporter.get_traceback_html() if self.include_html else None
  111. mail.mail_admins(subject, message, fail_silently=True,
  112. html_message=html_message,
  113. connection=self.connection())
  114. def connection(self):
  115. return get_connection(backend=self.email_backend, fail_silently=True)
  116. def format_subject(self, subject):
  117. """
  118. Escape CR and LF characters, and limit length.
  119. RFC 2822's hard limit is 998 characters per line. So, minus "Subject: "
  120. the actual subject must be no longer than 989 characters.
  121. """
  122. formatted_subject = subject.replace('\n', '\\n').replace('\r', '\\r')
  123. return formatted_subject[:989]
  124. class CallbackFilter(logging.Filter):
  125. """
  126. A logging filter that checks the return value of a given callable (which
  127. takes the record-to-be-logged as its only parameter) to decide whether to
  128. log a record.
  129. """
  130. def __init__(self, callback):
  131. self.callback = callback
  132. def filter(self, record):
  133. if self.callback(record):
  134. return 1
  135. return 0
  136. class RequireDebugFalse(logging.Filter):
  137. def filter(self, record):
  138. return not settings.DEBUG
  139. class RequireDebugTrue(logging.Filter):
  140. def filter(self, record):
  141. return settings.DEBUG