雖然沒有更好的選擇,但這是我實施的解決方案。
我已經設置了一個自定義的AdminEmailHandler來檢查的異常,並在數據庫的情況下OperationalError跳過異常,發送和錯誤,而不是。
import logging
from django.utils.log import AdminEmailHandler
from django.db.utils import OperationalError
logger = logging.getLogger('mylogger')
class CustomAdminEmailHandler(AdminEmailHandler):
# When mail is because of exception conencting to DB, avoid rendering the email,
# rendering email makes connections to DB making the query hang in multiple DB
# connection timeouts.
if record.exc_info:
exc_type, exc_value, exc_traceback = record.exc_info
if exc_type == OperationalError:
logger.error(exc_value, exc_info=False)
return
super(CustomAdminEmailHandler, self).emit(record)
而且在settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(asctime)s %(levelname)s %(module)s:%(funcName)s() %(message)s'
},
},
'handlers': {
'send_mail': {
'level': 'ERROR',
'class': 'myapp.handlers.CustomdAdminEmailHandler',
'include_html': False,
},
},
'loggers': {
'django': {
'handlers': ['send_mail'],
'level': 'INFO'
},
'mylogger': {
'handlers': ['send_mail'],
'level': 'INFO'
},
}
}
我還建立數據庫連接超時1秒,我不想請求,該數據庫不能訪問的情況下排隊,這當連接返回可能導致服務器關閉時,可能會導致非常高的負載。