This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
forked from felipelerena/FlightDeck
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding admin_log email handler for errors
- Loading branch information
1 parent
6be0e63
commit 32b6203
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
path = lambda *a: os.path.join(ROOT, *a) | ||
|
||
# Set the project version | ||
PROJECT_VERSION = "0.9.6b" | ||
PROJECT_VERSION = "0.9.9b" | ||
|
||
# TODO: This should be handled by prod in a settings_local. By default, we | ||
# shouldn't be in prod mode | ||
|
@@ -26,7 +26,8 @@ | |
ADMINS = ( | ||
('clouserw', '[email protected]'), | ||
('zalun', '[email protected]'), | ||
('dbuc', '[email protected]') | ||
('dbuc', '[email protected]'), | ||
('seanmonstar', '[email protected]'), | ||
) | ||
MANAGERS = ADMINS | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
This is from Django utils/log.py 3fe4e7a2~1. | ||
""" | ||
import logging | ||
import sys | ||
from django.core import mail | ||
|
||
# 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 | ||
|
||
if sys.version_info < (2, 5): | ||
class LoggerCompat(object): | ||
def __init__(self, logger): | ||
self._logger = logger | ||
|
||
def __getattr__(self, name): | ||
val = getattr(self._logger, name) | ||
if callable(val): | ||
def _wrapper(*args, **kwargs): | ||
# Python 2.4 logging module doesn't support 'extra' parameter to | ||
# methods of Logger | ||
kwargs.pop('extra', None) | ||
return val(*args, **kwargs) | ||
return _wrapper | ||
else: | ||
return val | ||
|
||
def getLogger(name=None): | ||
return LoggerCompat(logging.getLogger(name=name)) | ||
else: | ||
getLogger = logging.getLogger | ||
|
||
# Ensure the creation of the Django logger | ||
# with a null handler. This ensures we don't get any | ||
# 'No handlers could be found for logger "django"' messages | ||
logger = getLogger('django') | ||
if not logger.handlers: | ||
logger.addHandler(NullHandler()) | ||
|
||
class AdminEmailHandler(logging.Handler): | ||
"""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 | ||
""" | ||
def emit(self, record): | ||
import traceback | ||
from django.conf import settings | ||
|
||
try: | ||
if sys.version_info < (2,5): | ||
# A nasty workaround required because Python 2.4's logging | ||
# module doesn't support passing in extra context. | ||
# For this handler, the only extra data we need is the | ||
# request, and that's in the top stack frame. | ||
request = record.exc_info[2].tb_frame.f_locals['request'] | ||
else: | ||
request = record.request | ||
|
||
subject = '%s (%s IP): %s' % ( | ||
record.levelname, | ||
(request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), | ||
request.path | ||
) | ||
request_repr = repr(request) | ||
except: | ||
subject = 'Error: Unknown URL' | ||
request_repr = "Request repr() unavailable" | ||
|
||
if record.exc_info: | ||
stack_trace = '\n'.join(traceback.format_exception(*record.exc_info)) | ||
else: | ||
stack_trace = 'No stack trace available' | ||
|
||
message = "%s\n\n%s" % (stack_trace, request_repr) | ||
mail.mail_admins(subject, message, fail_silently=True) | ||
|