2014-10-07 04:07:20 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
import logging
|
2013-12-31 19:28:01 +08:00
|
|
|
import sys
|
|
|
|
import warnings
|
2011-06-09 06:18:46 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
2010-10-04 23:12:39 +08:00
|
|
|
from django.core import mail
|
2012-11-20 01:57:40 +08:00
|
|
|
from django.core.mail import get_connection
|
2014-03-08 18:13:45 +08:00
|
|
|
from django.utils.deprecation import RemovedInNextVersionWarning
|
2014-10-07 04:07:20 +08:00
|
|
|
from django.utils.encoding import force_text
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2011-06-09 06:18:46 +08:00
|
|
|
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
# Default logging for Django. This sends an email to the site admins on every
|
|
|
|
# HTTP 500 error. Depending on DEBUG, all other log records are either sent to
|
|
|
|
# the console (DEBUG=True) or discarded by mean of the NullHandler (DEBUG=False).
|
2012-09-25 04:30:38 +08:00
|
|
|
DEFAULT_LOGGING = {
|
|
|
|
'version': 1,
|
|
|
|
'disable_existing_loggers': False,
|
|
|
|
'filters': {
|
|
|
|
'require_debug_false': {
|
|
|
|
'()': 'django.utils.log.RequireDebugFalse',
|
2012-09-27 01:56:21 +08:00
|
|
|
},
|
|
|
|
'require_debug_true': {
|
|
|
|
'()': 'django.utils.log.RequireDebugTrue',
|
|
|
|
},
|
2012-09-25 04:30:38 +08:00
|
|
|
},
|
|
|
|
'handlers': {
|
2012-11-13 04:19:11 +08:00
|
|
|
'console': {
|
2012-09-27 01:56:21 +08:00
|
|
|
'level': 'INFO',
|
2012-09-30 05:45:28 +08:00
|
|
|
'filters': ['require_debug_true'],
|
2012-09-27 01:56:21 +08:00
|
|
|
'class': 'logging.StreamHandler',
|
|
|
|
},
|
2012-09-25 04:30:38 +08:00
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'filters': ['require_debug_false'],
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'django': {
|
2015-03-24 06:17:43 +08:00
|
|
|
'handlers': ['console', 'mail_admins'],
|
2012-09-25 04:30:38 +08:00
|
|
|
},
|
2012-11-17 08:50:50 +08:00
|
|
|
'py.warnings': {
|
|
|
|
'handlers': ['console'],
|
|
|
|
},
|
2012-09-25 04:30:38 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2011-09-17 00:41:38 +08:00
|
|
|
|
2013-12-31 19:28:01 +08:00
|
|
|
def configure_logging(logging_config, logging_settings):
|
|
|
|
if not sys.warnoptions:
|
|
|
|
# Route warnings through python logging
|
|
|
|
logging.captureWarnings(True)
|
2014-03-08 18:13:45 +08:00
|
|
|
# RemovedInNextVersionWarning is a subclass of DeprecationWarning which
|
|
|
|
# is hidden by default, hence we force the "default" behavior
|
|
|
|
warnings.simplefilter("default", RemovedInNextVersionWarning)
|
2013-12-31 19:28:01 +08:00
|
|
|
|
|
|
|
if logging_config:
|
2014-03-31 03:11:05 +08:00
|
|
|
# First find the logging configuration function ...
|
2014-01-21 04:15:14 +08:00
|
|
|
logging_config_func = import_string(logging_config)
|
2013-12-31 19:28:01 +08:00
|
|
|
|
2015-02-06 07:30:11 +08:00
|
|
|
logging.config.dictConfig(DEFAULT_LOGGING)
|
2013-12-31 19:28:01 +08:00
|
|
|
|
|
|
|
# ... then invoke it with the logging settings
|
|
|
|
if logging_settings:
|
|
|
|
logging_config_func(logging_settings)
|
|
|
|
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
class AdminEmailHandler(logging.Handler):
|
2011-04-02 00:10:22 +08:00
|
|
|
"""An exception log handler that emails log entries to site admins.
|
2010-10-04 23:12:39 +08:00
|
|
|
|
|
|
|
If the request is passed as the first argument to the log record,
|
2011-06-09 06:18:46 +08:00
|
|
|
request data will be provided in the email report.
|
2010-10-04 23:12:39 +08:00
|
|
|
"""
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2012-11-20 01:57:40 +08:00
|
|
|
def __init__(self, include_html=False, email_backend=None):
|
2011-09-02 11:04:02 +08:00
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.include_html = include_html
|
2012-11-20 01:57:40 +08:00
|
|
|
self.email_backend = email_backend
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
def emit(self, record):
|
|
|
|
try:
|
2011-03-28 10:11:19 +08:00
|
|
|
request = record.request
|
2010-10-04 23:12:39 +08:00
|
|
|
subject = '%s (%s IP): %s' % (
|
|
|
|
record.levelname,
|
2013-05-27 10:47:50 +08:00
|
|
|
('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
|
|
|
|
else 'EXTERNAL'),
|
2012-02-12 20:19:02 +08:00
|
|
|
record.getMessage()
|
2010-10-04 23:12:39 +08:00
|
|
|
)
|
2011-06-09 06:18:46 +08:00
|
|
|
filter = get_exception_reporter_filter(request)
|
2014-11-27 08:41:27 +08:00
|
|
|
request_repr = '\n{}'.format(force_text(filter.get_request_repr(request)))
|
2012-02-11 17:31:18 +08:00
|
|
|
except Exception:
|
2011-03-17 18:33:08 +08:00
|
|
|
subject = '%s: %s' % (
|
|
|
|
record.levelname,
|
2011-09-02 11:04:02 +08:00
|
|
|
record.getMessage()
|
2011-03-17 18:33:08 +08:00
|
|
|
)
|
2010-12-06 22:21:51 +08:00
|
|
|
request = None
|
2013-09-12 22:04:28 +08:00
|
|
|
request_repr = "unavailable"
|
2012-02-11 17:31:18 +08:00
|
|
|
subject = self.format_subject(subject)
|
2010-10-04 23:12:39 +08:00
|
|
|
|
|
|
|
if record.exc_info:
|
2010-12-06 22:21:51 +08:00
|
|
|
exc_info = record.exc_info
|
2010-10-04 23:12:39 +08:00
|
|
|
else:
|
2011-09-02 11:04:02 +08:00
|
|
|
exc_info = (None, record.getMessage(), None)
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2013-09-12 22:04:28 +08:00
|
|
|
message = "%s\n\nRequest repr(): %s" % (self.format(record), request_repr)
|
2010-12-07 05:54:49 +08:00
|
|
|
reporter = ExceptionReporter(request, is_email=True, *exc_info)
|
2013-05-17 22:33:36 +08:00
|
|
|
html_message = reporter.get_traceback_html() if self.include_html else None
|
2014-11-11 05:21:31 +08:00
|
|
|
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
|
|
|
|
|
|
|
|
def send_mail(self, subject, message, *args, **kwargs):
|
|
|
|
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
|
2012-11-20 01:57:40 +08:00
|
|
|
|
|
|
|
def connection(self):
|
2013-01-22 03:27:38 +08:00
|
|
|
return get_connection(backend=self.email_backend, fail_silently=True)
|
2011-06-22 14:01:44 +08:00
|
|
|
|
2012-02-11 17:31:18 +08:00
|
|
|
def format_subject(self, subject):
|
|
|
|
"""
|
|
|
|
Escape CR and LF characters, and limit length.
|
|
|
|
RFC 2822's hard limit is 998 characters per line. So, minus "Subject: "
|
|
|
|
the actual subject must be no longer than 989 characters.
|
|
|
|
"""
|
|
|
|
formatted_subject = subject.replace('\n', '\\n').replace('\r', '\\r')
|
|
|
|
return formatted_subject[:989]
|
|
|
|
|
2011-06-22 14:01:44 +08:00
|
|
|
|
|
|
|
class CallbackFilter(logging.Filter):
|
|
|
|
"""
|
|
|
|
A logging filter that checks the return value of a given callable (which
|
|
|
|
takes the record-to-be-logged as its only parameter) to decide whether to
|
|
|
|
log a record.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, callback):
|
|
|
|
self.callback = callback
|
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
if self.callback(record):
|
|
|
|
return 1
|
|
|
|
return 0
|
2011-09-17 00:41:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RequireDebugFalse(logging.Filter):
|
|
|
|
def filter(self, record):
|
|
|
|
return not settings.DEBUG
|
2012-09-27 01:56:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RequireDebugTrue(logging.Filter):
|
|
|
|
def filter(self, record):
|
2012-12-25 05:32:41 +08:00
|
|
|
return settings.DEBUG
|