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.

log.py 5.1 KiB

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