2010-10-04 23:12:39 +08:00
|
|
|
import logging
|
2011-06-09 06:18:46 +08:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
from django.conf import settings
|
2010-10-04 23:12:39 +08:00
|
|
|
from django.core import mail
|
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-25 04:30:38 +08:00
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
# Make sure a NullHandler is available
|
|
|
|
# This was added in Python 2.7/3.2
|
|
|
|
try:
|
|
|
|
from logging import NullHandler
|
|
|
|
except ImportError:
|
|
|
|
class NullHandler(logging.Handler):
|
|
|
|
def emit(self, record):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Make sure that dictConfig is available
|
|
|
|
# This was added in Python 2.7/3.2
|
|
|
|
try:
|
|
|
|
from logging.config import dictConfig
|
|
|
|
except ImportError:
|
|
|
|
from django.utils.dictconfig import dictConfig
|
|
|
|
|
2011-03-28 10:11:19 +08:00
|
|
|
getLogger = logging.getLogger
|
2010-10-06 23:02:26 +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
|
|
|
'null': {
|
|
|
|
'class': 'django.utils.log.NullHandler',
|
|
|
|
},
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'filters': ['require_debug_false'],
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'django': {
|
2012-09-27 01:56:21 +08:00
|
|
|
'handlers': ['console'],
|
2012-09-25 04:30:38 +08:00
|
|
|
},
|
|
|
|
'django.request': {
|
|
|
|
'handlers': ['mail_admins'],
|
|
|
|
'level': 'ERROR',
|
2012-09-27 01:56:21 +08:00
|
|
|
'propagate': False,
|
2012-09-25 04:30:38 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2011-09-17 00:41:38 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
def __init__(self, include_html=False):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.include_html = include_html
|
|
|
|
|
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,
|
2012-02-11 17:31:18 +08:00
|
|
|
(request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
|
|
|
|
and 'internal' or '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)
|
|
|
|
request_repr = 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
|
2011-06-09 06:18:46 +08:00
|
|
|
request_repr = "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
|
|
|
stack_trace = '\n'.join(traceback.format_exception(*record.exc_info))
|
|
|
|
else:
|
2011-09-02 11:04:02 +08:00
|
|
|
exc_info = (None, record.getMessage(), None)
|
2010-10-04 23:12:39 +08:00
|
|
|
stack_trace = 'No stack trace available'
|
|
|
|
|
|
|
|
message = "%s\n\n%s" % (stack_trace, request_repr)
|
2010-12-07 05:54:49 +08:00
|
|
|
reporter = ExceptionReporter(request, is_email=True, *exc_info)
|
2011-03-16 12:13:57 +08:00
|
|
|
html_message = self.include_html and reporter.get_traceback_html() or None
|
2011-03-28 10:11:19 +08:00
|
|
|
mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)
|
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):
|
|
|
|
return settings.DEBUG
|