2014-10-07 04:07:20 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
import logging
|
2015-05-08 00:11:39 +08:00
|
|
|
import logging.config # needed when logging_config doesn't start with logging.config
|
2013-12-31 19:28:01 +08:00
|
|
|
import sys
|
|
|
|
import warnings
|
2015-07-09 23:27:07 +08:00
|
|
|
from copy import copy
|
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
|
2015-11-07 00:19:41 +08:00
|
|
|
from django.core.management.color import color_style
|
2014-03-08 18:13:45 +08:00
|
|
|
from django.utils.deprecation import RemovedInNextVersionWarning
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2015-07-09 23:27:07 +08:00
|
|
|
from django.views.debug import ExceptionReporter
|
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
|
2015-11-07 23:07:21 +08:00
|
|
|
# the console (DEBUG=True) or discarded (DEBUG=False) by means of the
|
|
|
|
# require_debug_true filter.
|
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
|
|
|
},
|
2015-11-07 00:19:41 +08:00
|
|
|
'formatters': {
|
|
|
|
'django.server': {
|
|
|
|
'()': 'django.utils.log.ServerFormatter',
|
|
|
|
'format': '[%(server_time)s] %(message)s',
|
|
|
|
}
|
|
|
|
},
|
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',
|
|
|
|
},
|
2015-11-07 00:19:41 +08:00
|
|
|
'django.server': {
|
|
|
|
'level': 'INFO',
|
|
|
|
'class': 'logging.StreamHandler',
|
|
|
|
'formatter': 'django.server',
|
|
|
|
},
|
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'],
|
2015-09-16 02:13:34 +08:00
|
|
|
'level': 'INFO',
|
2012-09-25 04:30:38 +08:00
|
|
|
},
|
2015-11-07 00:19:41 +08:00
|
|
|
'django.server': {
|
|
|
|
'handlers': ['django.server'],
|
|
|
|
'level': 'INFO',
|
|
|
|
'propagate': False,
|
|
|
|
},
|
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
|
|
|
)
|
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
|
2012-02-11 17:31:18 +08:00
|
|
|
subject = self.format_subject(subject)
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2015-07-09 23:27:07 +08:00
|
|
|
# Since we add a nicely formatted traceback on our own, create a copy
|
|
|
|
# of the log record without the exception data.
|
|
|
|
no_exc_record = copy(record)
|
|
|
|
no_exc_record.exc_info = None
|
|
|
|
no_exc_record.exc_text = None
|
|
|
|
|
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
|
|
|
|
2010-12-07 05:54:49 +08:00
|
|
|
reporter = ExceptionReporter(request, is_email=True, *exc_info)
|
2015-07-09 23:27:07 +08:00
|
|
|
message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text())
|
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
|
2015-11-07 00:19:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ServerFormatter(logging.Formatter):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.style = color_style()
|
|
|
|
super(ServerFormatter, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
args = record.args
|
|
|
|
msg = record.msg
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
msg = self.style.HTTP_BAD_REQUEST(msg)
|
|
|
|
else:
|
|
|
|
if args[1][0] == '2':
|
|
|
|
# Put 2XX first, since it should be the common case
|
|
|
|
msg = self.style.HTTP_SUCCESS(msg)
|
|
|
|
elif args[1][0] == '1':
|
|
|
|
msg = self.style.HTTP_INFO(msg)
|
|
|
|
elif args[1] == '304':
|
|
|
|
msg = self.style.HTTP_NOT_MODIFIED(msg)
|
|
|
|
elif args[1][0] == '3':
|
|
|
|
msg = self.style.HTTP_REDIRECT(msg)
|
|
|
|
elif args[1] == '404':
|
|
|
|
msg = self.style.HTTP_NOT_FOUND(msg)
|
|
|
|
elif args[1][0] == '4':
|
|
|
|
msg = self.style.HTTP_BAD_REQUEST(msg)
|
|
|
|
else:
|
|
|
|
# Any 5XX, or any other response
|
|
|
|
msg = self.style.HTTP_SERVER_ERROR(msg)
|
|
|
|
|
|
|
|
record.msg = msg
|
|
|
|
return super(ServerFormatter, self).format(record)
|