当Django settings.py中的DEBUG = False时,Raven / Sentry无法正常工作

时间:2013-10-27 19:09:47

标签: python django sentry raven

我安装了Sentry(6.3.2)/ Raven(3.5.1)来监控我的Django 1.5.5应用程序,如果我在settings.py中将我的应用程序设置为DEBUG = True,则该程序可以正常运行。但是,如果我将其设置为False,则不会再向Sentry发送错误消息。使用“python manage.py raven test”(除了在调试模式下运行Django时出现的错误消息),我的Raven应用程序肯定有效。 Sentry在与我的Django应用程序不同的虚拟环境中运行,它们都运行在Nginx反向代理中。我正在运行python 2.6。

我的settings.py看起来像这样:

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ADMINS = (
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'my_db',                      # Or path to database file if using sqlite3.
        'USER': 'my_db_user',                      # Not used with sqlite3.
        'PASSWORD': 'my_db_pw',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

EMAIL_HOST = "smtp.gmail.com" 
EMAIL_HOST_USER = "my_mail@my_provider.com"
EMAIL_HOST_PASSWORD = "my_password"
EMAIL_PORT = 25
EMAIL_USE_TLS = True

ALLOWED_HOSTS = ['my_domain', 'localhost']

TIME_ZONE = 'Europe/Zurich'

LANGUAGE_CODE = 'de-ch'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

USE_TZ = True

USE_THOUSAND_SEPARATOR = True

FORMAT_MODULE_PATH = 'my_app.formats'

MEDIA_ROOT = ''

MEDIA_URL = '/media/'

STATIC_ROOT = '/path/to/static'

STATIC_URL = 'http://myip/static/'

STATICFILES_DIRS = (
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

SECRET_KEY = 'my_secret_key'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
                               "django.core.context_processors.debug",
                               "django.core.context_processors.i18n",
                               "django.core.context_processors.media",
                               "django.core.context_processors.static",
                               "django.core.context_processors.request",
                               "django.contrib.messages.context_processors.messages")

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'

ROOT_URLCONF = 'my_app.urls'

WSGI_APPLICATION = 'my_app.wsgi.application'

TEMPLATE_DIRS = (
    '/paht/to/templates',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sitemaps',
    'django_extensions',
    'south',
    'django.contrib.admin',
    'raven.contrib.django.raven_compat',
)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
    },
}

RAVEN_CONFIG = {
    'dsn': 'http://some_code@www.my_domain.com/sentry/2',
}

和我的sentry.conf.py这样:

from sentry.conf.server import *
import os.path

CONF_ROOT = os.path.dirname(__file__)

DATABASES = {
    'default': {

        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'my_sentry_db',
        'USER': 'my_sentry_db_user',
        'PASSWORD': 'my_sentry_db_pw',
        'HOST': 'localhost',
        'PORT': '',

    }
}

SENTRY_URL_PREFIX = '/sentry'
FORCE_SCRIPT_NAME = '/sentry'

ALLOWED_HOSTS = ['www.my_domain.com', 'localhost', '127.0.0.1:9002']

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SENTRY_WEB_HOST = '0.0.0.0'
SENTRY_WEB_PORT = 9002
SENTRY_WEB_OPTIONS = {
    'workers': 3,  # the number of gunicorn workers
    'secure_scheme_headers': {'X-FORWARDED-PROTO': 'https'},
}

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'localhost'
EMAIL_HOST_PASSWORD = ''
EMAIL_HOST_USER = ''
EMAIL_PORT = 25
EMAIL_USE_TLS = False

SERVER_EMAIL = 'root@sentry'

SECRET_KEY = 'my_secret_key'

TWITTER_CONSUMER_KEY = ''
TWITTER_CONSUMER_SECRET = ''

FACEBOOK_APP_ID = ''
FACEBOOK_API_SECRET = ''

GOOGLE_OAUTH2_CLIENT_ID = ''
GOOGLE_OAUTH2_CLIENT_SECRET = ''

GITHUB_APP_ID = ''
GITHUB_API_SECRET = ''

TRELLO_API_KEY = ''
TRELLO_API_SECRET = ''

BITBUCKET_CONSUMER_KEY = ''
BITBUCKET_CONSUMER_SECRET = ''

如何在不使用Django的调试模式下运行Raven / Sentry?

非常感谢您的帮助!

更新 我只是想补充一点,我确实收到“SuspiciousOperation:无效的HTTP _ HOST标题(您可能需要设置ALLOWED _ HOSTS) “Sentry上的错误消息,即使调试模式已关闭。

这些消息中的大部分来自于我从ALLOWED_HOSTS列表中删除了我之前的域名,而机器人仍在寻找它。

并且只是为了澄清:我在非调试模式下没有收到的消息是我在我的代码中放入“断言错误”或只是输入一些随机字符串给我一个错误的消息。

0 个答案:

没有答案