An exception log handler that emails log entries to site admins. If the request is passed as the first argument to the log record, request data will be provided in the email report.
| 79 | |
| 80 | |
| 81 | class AdminEmailHandler(logging.Handler): |
| 82 | """An exception log handler that emails log entries to site admins. |
| 83 | |
| 84 | If the request is passed as the first argument to the log record, |
| 85 | request data will be provided in the email report. |
| 86 | """ |
| 87 | |
| 88 | def __init__( |
| 89 | self, include_html=False, email_backend=None, reporter_class=None, using=None |
| 90 | ): |
| 91 | super().__init__() |
| 92 | |
| 93 | # RemovedInDjango70Warning: email_backend arg and connection error. |
| 94 | if email_backend: |
| 95 | if using: |
| 96 | raise ImproperlyConfigured( |
| 97 | "The 'email_backend' argument is not compatible with 'using'." |
| 98 | ) |
| 99 | if mail.mailers._is_configured: |
| 100 | raise ImproperlyConfigured( |
| 101 | "The 'email_backend' argument is not valid when " |
| 102 | "settings.MAILERS is defined." |
| 103 | ) |
| 104 | warnings.warn( |
| 105 | "The 'email_backend' argument is deprecated. Use 'using' instead.", |
| 106 | RemovedInDjango70Warning, |
| 107 | skip_file_prefixes=django_file_prefixes(), |
| 108 | ) |
| 109 | if hasattr(self, "connection"): |
| 110 | raise AttributeError( |
| 111 | "The undocumented AdminEmailHandler.connection() method is no longer " |
| 112 | "used." |
| 113 | ) |
| 114 | |
| 115 | self.include_html = include_html |
| 116 | self.email_backend = email_backend |
| 117 | self.using = using |
| 118 | self.reporter_class = import_string( |
| 119 | reporter_class or settings.DEFAULT_EXCEPTION_REPORTER |
| 120 | ) |
| 121 | |
| 122 | def emit(self, record): |
| 123 | # Early return when no email will be sent. |
| 124 | if ( |
| 125 | not settings.ADMINS |
| 126 | # Method not overridden. |
| 127 | and self.send_mail.__func__ is AdminEmailHandler.send_mail |
| 128 | ): |
| 129 | return |
| 130 | try: |
| 131 | request = record.request |
| 132 | subject = "%s (%s IP): %s" % ( |
| 133 | record.levelname, |
| 134 | ( |
| 135 | "internal" |
| 136 | if request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS |
| 137 | else "EXTERNAL" |
| 138 | ), |
no outgoing calls