4

我已经安装了 Sentry(6.3.2)/Raven(3.5.1) 来监控我的 Django 1.5.5 应用程序,如果我的应用程序在我的 settings.py 中设置为 DEBUG=True,它就可以工作。但是,如果我将其设置为 False,则不再有错误消息到达 Sentry。我的 Raven 应用程序在使用“python manage.py raven test”进行测试时肯定可以工作(除了在调试模式下运行 Django 时出现的错误消息)。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 运行?

非常感谢您的帮助!

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

这些消息中的大部分来自这样一个事实,即我已从我的 ALLOWED_HOSTS 列表中删除了我以前的一个域,并且机器人仍在寻找它。

只是为了澄清一下:我在非调试模式下没有收到的消息是我在我的代码中放入“assert False”或者只是输入一些随机字符串来抛出错误的消息。

4

0 回答 0