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.

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import logging
  2. import logging.config # needed when logging_config doesn't start with logging.config
  3. from copy import copy
  4. from django.conf import settings
  5. from django.core import mail
  6. from django.core.mail import get_connection
  7. from django.core.management.color import color_style
  8. from django.utils.module_loading import import_string
  9. from django.views.debug import ExceptionReporter
  10. request_logger = logging.getLogger('django.request')
  11. # Default logging for Django. This sends an email to the site admins on every
  12. # HTTP 500 error. Depending on DEBUG, all other log records are either sent to
  13. # the console (DEBUG=True) or discarded (DEBUG=False) by means of the
  14. # require_debug_true filter.
  15. DEFAULT_LOGGING = {
  16. 'version': 1,
  17. 'disable_existing_loggers': False,
  18. 'filters': {
  19. 'require_debug_false': {
  20. '()': 'django.utils.log.RequireDebugFalse',
  21. },
  22. 'require_debug_true': {
  23. '()': 'django.utils.log.RequireDebugTrue',
  24. },
  25. },
  26. 'formatters': {
  27. 'django.server': {
  28. '()': 'django.utils.log.ServerFormatter',
  29. 'format': '[{server_time}] {message}',
  30. 'style': '{',
  31. }
  32. },
  33. 'handlers': {
  34. 'console': {
  35. 'level': 'INFO',
  36. 'filters': ['require_debug_true'],
  37. 'class': 'logging.StreamHandler',
  38. },
  39. 'django.server': {
  40. 'level': 'INFO',
  41. 'class': 'logging.StreamHandler',
  42. 'formatter': 'django.server',
  43. },
  44. 'mail_admins': {
  45. 'level': 'ERROR',
  46. 'filters': ['require_debug_false'],
  47. 'class': 'django.utils.log.AdminEmailHandler'
  48. }
  49. },
  50. 'loggers': {
  51. 'django': {
  52. 'handlers': ['console', 'mail_admins'],
  53. 'level': 'INFO',
  54. },
  55. 'django.server': {
  56. 'handlers': ['django.server'],
  57. 'level': 'INFO',
  58. 'propagate': False,
  59. },
  60. }
  61. }
  62. def configure_logging(logging_config, logging_settings):
  63. if logging_config:
  64. # First find the logging configuration function ...
  65. logging_config_func = import_string(logging_config)
  66. logging.config.dictConfig(DEFAULT_LOGGING)
  67. # ... then invoke it with the logging settings
  68. if logging_settings:
  69. logging_config_func(logging_settings)
  70. class AdminEmailHandler(logging.Handler):
  71. """An exception log handler that emails log entries to site admins.
  72. If the request is passed as the first argument to the log record,
  73. request data will be provided in the email report.
  74. """
  75. def __init__(self, include_html=False, email_backend=None):
  76. super().__init__()
  77. self.include_html = include_html
  78. self.email_backend = email_backend
  79. def emit(self, record):
  80. try:
  81. request = record.request
  82. subject = '%s (%s IP): %s' % (
  83. record.levelname,
  84. ('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
  85. else 'EXTERNAL'),
  86. record.getMessage()
  87. )
  88. except Exception:
  89. subject = '%s: %s' % (
  90. record.levelname,
  91. record.getMessage()
  92. )
  93. request = None
  94. subject = self.format_subject(subject)
  95. # Since we add a nicely formatted traceback on our own, create a copy
  96. # of the log record without the exception data.
  97. no_exc_record = copy(record)
  98. no_exc_record.exc_info = None
  99. no_exc_record.exc_text = None
  100. if record.exc_info:
  101. exc_info = record.exc_info
  102. else:
  103. exc_info = (None, record.getMessage(), None)
  104. reporter = ExceptionReporter(request, is_email=True, *exc_info)
  105. message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text())
  106. html_message = reporter.get_traceback_html() if self.include_html else None
  107. self.send_mail(subject, message, fail_silently=True, html_message=html_message)
  108. def send_mail(self, subject, message, *args, **kwargs):
  109. mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
  110. def connection(self):
  111. return get_connection(backend=self.email_backend, fail_silently=True)
  112. def format_subject(self, subject):
  113. """
  114. Escape CR and LF characters.
  115. """
  116. return subject.replace('\n', '\\n').replace('\r', '\\r')
  117. class CallbackFilter(logging.Filter):
  118. """
  119. A logging filter that checks the return value of a given callable (which
  120. takes the record-to-be-logged as its only parameter) to decide whether to
  121. log a record.
  122. """
  123. def __init__(self, callback):
  124. self.callback = callback
  125. def filter(self, record):
  126. if self.callback(record):
  127. return 1
  128. return 0
  129. class RequireDebugFalse(logging.Filter):
  130. def filter(self, record):
  131. return not settings.DEBUG
  132. class RequireDebugTrue(logging.Filter):
  133. def filter(self, record):
  134. return settings.DEBUG
  135. class ServerFormatter(logging.Formatter):
  136. def __init__(self, *args, **kwargs):
  137. self.style = color_style()
  138. super().__init__(*args, **kwargs)
  139. def format(self, record):
  140. msg = record.msg
  141. status_code = getattr(record, 'status_code', None)
  142. if status_code:
  143. if 200 <= status_code < 300:
  144. # Put 2XX first, since it should be the common case
  145. msg = self.style.HTTP_SUCCESS(msg)
  146. elif 100 <= status_code < 200:
  147. msg = self.style.HTTP_INFO(msg)
  148. elif status_code == 304:
  149. msg = self.style.HTTP_NOT_MODIFIED(msg)
  150. elif 300 <= status_code < 400:
  151. msg = self.style.HTTP_REDIRECT(msg)
  152. elif status_code == 404:
  153. msg = self.style.HTTP_NOT_FOUND(msg)
  154. elif 400 <= status_code < 500:
  155. msg = self.style.HTTP_BAD_REQUEST(msg)
  156. else:
  157. # Any 5XX, or any other status code
  158. msg = self.style.HTTP_SERVER_ERROR(msg)
  159. if self.uses_server_time() and not hasattr(record, 'server_time'):
  160. record.server_time = self.formatTime(record, self.datefmt)
  161. record.msg = msg
  162. return super().format(record)
  163. def uses_server_time(self):
  164. return self._fmt.find('{server_time}') >= 0
  165. def log_response(message, *args, response=None, request=None, logger=request_logger, level=None, exc_info=None):
  166. """
  167. Log errors based on HttpResponse status.
  168. Log 5xx responses as errors and 4xx responses as warnings (unless a level
  169. is given as a keyword argument). The HttpResponse status_code and the
  170. request are passed to the logger's extra parameter.
  171. """
  172. # Check if the response has already been logged. Multiple requests to log
  173. # the same response can be received in some cases, e.g., when the
  174. # response is the result of an exception and is logged at the time the
  175. # exception is caught so that the exc_info can be recorded.
  176. if getattr(response, '_has_been_logged', False):
  177. return
  178. if level is None:
  179. if response.status_code >= 500:
  180. level = 'error'
  181. elif response.status_code >= 400:
  182. level = 'warning'
  183. else:
  184. level = 'info'
  185. getattr(logger, level)(
  186. message, *args,
  187. extra={
  188. 'status_code': response.status_code,
  189. 'request': request,
  190. },
  191. exc_info=exc_info,
  192. )
  193. response._has_been_logged = True