Implement basic email error reporting.

Here we send an email for each unique error every ten minutes.

(imported from commit adf5ee4bf52c9aef253a94b1c3647515d9b3e495)
This commit is contained in:
Luke Faraone 2012-12-05 12:01:43 -05:00
parent d04758f5f1
commit adf289c9df
2 changed files with 51 additions and 2 deletions

35
humbug/ratelimit.py Normal file
View File

@ -0,0 +1,35 @@
import traceback
from hashlib import sha256
from datetime import datetime, timedelta
# Adapted http://djangosnippets.org/snippets/2242/ by user s29 (October 25, 2010)
class RateLimitFilter(object):
last_error = 0
def filter(self, record):
from django.conf import settings
from django.core.cache import cache
# Track duplicate errors
duplicate = False
rate = getattr(settings, 'ERROR_RATE_LIMIT', 600) # seconds
if rate > 0:
# Test if the cache works
try:
cache.set('RLF_TEST_KEY', 1, 1)
use_cache = cache.get('RLF_TEST_KEY') == 1
except:
use_cache = False
if use_cache:
duplicate = cache.get('ERROR_RATE') == 1
cache.set('ERROR_RATE', 1, rate)
else:
min_date = datetime.now() - timedelta(seconds=rate)
duplicate = (self.last_error >= min_date)
if not duplicate:
self.last_error = datetime.now()
return not duplicate

View File

@ -154,6 +154,7 @@ if deployed:
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 3600
} }
error_filters = ['ratelimit']
else:
CACHES = { 'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
@ -163,6 +164,9 @@ else:
'MAX_ENTRIES': 100000
}
} }
error_filters = []
ERROR_RATE_LIMIT=600
LOGGING = {
'version': 1,
@ -172,6 +176,11 @@ LOGGING = {
'format': '%(asctime)s %(levelname)-8s %(message)s'
}
},
'filters': {
'ratelimit': {
'()': 'humbug.ratelimit.RateLimitFilter',
}
},
'handlers': {
'console': {
'level': 'DEBUG',
@ -183,11 +192,16 @@ LOGGING = {
'class': 'logging.FileHandler',
'formatter': 'default',
'filename': 'server.log'
}
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'filters': error_filters,
},
},
'loggers': {
'': {
'handlers': ['console', 'file'],
'handlers': ['console', 'file', 'mail_admins'],
'level': 'INFO'
}
}