Python Django settings.py中DEBUG=False时Raven/Sentry不工作

Python Django settings.py中DEBUG=False时Raven/Sentry不工作,python,django,sentry,raven,Python,Django,Sentry,Raven,我已经安装了Sentry(6.3.2)/Raven(3.5.1)来监视我的Django 1.5.5应用程序,如果我的应用程序在我的settings.py中的DEBUG=True,它就会工作。然而,若我将其设置为False,就不会有更多的错误消息到达哨兵。当使用“python manage.py Raven test”进行测试时,我的Raven应用程序肯定能正常工作(除了在调试模式下运行Django时出现的错误消息)。 Sentry在与Django应用程序不同的虚拟环境中运行,它们都通过Nginx

我已经安装了Sentry(6.3.2)/Raven(3.5.1)来监视我的Django 1.5.5应用程序,如果我的应用程序在我的settings.py中的DEBUG=True,它就会工作。然而,若我将其设置为False,就不会有更多的错误消息到达哨兵。当使用“python manage.py Raven test”进行测试时,我的Raven应用程序肯定能正常工作(除了在调试模式下运行Django时出现的错误消息)。 Sentry在与Django应用程序不同的虚拟环境中运行,它们都通过Nginx反向代理运行。我正在运行python 2.6

My 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',
}
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 = ''
我的sentry.conf.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',
}
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 = ''
我如何让Raven/Sentry在不使用Django的调试模式下运行

非常感谢您的帮助

更新: 我只是想补充一点,我确实在Sentry上收到了“可疑操作:无效的HTTP_uu主机头(您可能需要设置允许的_uuu主机)”错误消息,即使调试模式已关闭

这些消息中的大多数来自我已经从我的允许的主机列表中删除了我以前的一个域,而机器人程序仍在寻找它


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

我遇到了这个问题。请尝试将
www.
www.my_domain.com
元素中删除
ALLOWED_HOSTS
条目,或者添加
*
一段时间,以消除
ALLOWED_HOSTS
导致此问题的原因。嘿,感谢您的提示,很抱歉我花了这么长时间才回答:所以我尝试将settings.py文件和sentry.conf.py文件中允许的_HOSTS地址替换为“*”和“0.0.0.0”(这是相同的,不是吗?),但没有改变行为。我想它现在的好处是,问题可能与Django 1.5中新的ALLOWED_HOSTS安全措施无关。你是如何解决这个问题的?再次感谢你的帮助!对我来说,这就像裸体域名一样简单。唉,我几乎可以肯定这会让你吃一口。嗯。因此,在非调试模式下,Sentry中间件遇到了一些障碍-当代码中生成异常时会发生什么,您会得到501?实际上,当我关闭调试模式时,会显示500条消息(服务器错误(500),所以它是标准的Django模板)。你知道还有什么可能导致这个乌鸦/哨兵问题吗?对不起,501是一个输入错误,500是我的意思,是正确的。很酷,如果你能从Django获得标准500,这看起来不错。对不起,我完全糊涂了,暂时没有主意了。如果我有其他的想法,我会把它贴出来,否则如果你想出来的话,请继续跟进。此时,我将向Django/Raven代码中添加调试语句,以了解为什么Raven的中间件没有捕捉到这些语句。看看你能不能强迫它,再隔离一个方面。