Skip to content

Commit f6f25e1

Browse files
committed
Merge pull request #175 from rambo/issue-172
Make sentry use configurable
2 parents 42333e7 + 9975be4 commit f6f25e1

File tree

3 files changed

+82
-52
lines changed

3 files changed

+82
-52
lines changed

project/config/settings/production.py

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
)
3737
RAVEN_MIDDLEWARE = ('raven.contrib.django.raven_compat.middleware.Sentry404CatchMiddleware',
3838
'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware',)
39-
MIDDLEWARE_CLASSES = SECURITY_MIDDLEWARE + \
40-
RAVEN_MIDDLEWARE + MIDDLEWARE_CLASSES
4139

40+
if env.bool('USE_SENTRY', True):
41+
MIDDLEWARE_CLASSES = SECURITY_MIDDLEWARE + RAVEN_MIDDLEWARE + MIDDLEWARE_CLASSES
42+
else:
43+
MIDDLEWARE_CLASSES = SECURITY_MIDDLEWARE + MIDDLEWARE_CLASSES
4244

4345
# set this to 60 seconds and then to 518400 when you can prove it works
4446
SECURE_HSTS_SECONDS = 60
@@ -85,60 +87,77 @@
8587

8688

8789
# Sentry Configuration
88-
SENTRY_DSN = env('DJANGO_SENTRY_DSN')
89-
SENTRY_CLIENT = env('DJANGO_SENTRY_CLIENT', default='raven.contrib.django.raven_compat.DjangoClient')
90-
LOGGING = {
91-
'version': 1,
92-
'disable_existing_loggers': True,
93-
'root': {
94-
'level': 'WARNING',
95-
'handlers': ['sentry'],
96-
},
97-
'formatters': {
98-
'verbose': {
99-
'format': '%(levelname)s %(asctime)s %(module)s '
100-
'%(process)d %(thread)d %(message)s'
90+
if env.bool('USE_SENTRY', True):
91+
SENTRY_DSN = env('DJANGO_SENTRY_DSN')
92+
SENTRY_CLIENT = env('DJANGO_SENTRY_CLIENT', default='raven.contrib.django.raven_compat.DjangoClient')
93+
LOGGING = {
94+
'version': 1,
95+
'disable_existing_loggers': True,
96+
'root': {
97+
'level': 'WARNING',
98+
'handlers': ['sentry'],
10199
},
102-
},
103-
'handlers': {
104-
'sentry': {
105-
'level': 'ERROR',
106-
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
100+
'formatters': {
101+
'verbose': {
102+
'format': '%(levelname)s %(asctime)s %(module)s '
103+
'%(process)d %(thread)d %(message)s'
104+
},
107105
},
108-
'console': {
109-
'level': 'DEBUG',
110-
'class': 'logging.StreamHandler',
111-
'formatter': 'verbose'
112-
}
113-
},
114-
'loggers': {
115-
'django.db.backends': {
116-
'level': 'ERROR',
117-
'handlers': ['console'],
118-
'propagate': False,
106+
'handlers': {
107+
'sentry': {
108+
'level': 'ERROR',
109+
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
110+
},
111+
'console': {
112+
'level': 'DEBUG',
113+
'class': 'logging.StreamHandler',
114+
'formatter': 'verbose'
115+
}
119116
},
120-
'raven': {
121-
'level': 'DEBUG',
122-
'handlers': ['console'],
123-
'propagate': False,
117+
'loggers': {
118+
'django.db.backends': {
119+
'level': 'ERROR',
120+
'handlers': ['console'],
121+
'propagate': False,
122+
},
123+
'raven': {
124+
'level': 'DEBUG',
125+
'handlers': ['console'],
126+
'propagate': False,
127+
},
128+
'sentry.errors': {
129+
'level': 'DEBUG',
130+
'handlers': ['console'],
131+
'propagate': False,
132+
},
133+
'django.security.DisallowedHost': {
134+
'level': 'ERROR',
135+
'handlers': ['console', 'sentry'],
136+
'propagate': False,
137+
},
124138
},
125-
'sentry.errors': {
126-
'level': 'DEBUG',
127-
'handlers': ['console'],
128-
'propagate': False,
139+
}
140+
SENTRY_CELERY_LOGLEVEL = env.int('DJANGO_SENTRY_LOG_LEVEL', logging.INFO)
141+
RAVEN_CONFIG = {
142+
'CELERY_LOGLEVEL': env.int('DJANGO_SENTRY_LOG_LEVEL', logging.INFO),
143+
'DSN': SENTRY_DSN
144+
}
145+
else:
146+
LOGGING = {
147+
'version': 1,
148+
'disable_existing_loggers': False,
149+
'handlers': {
150+
'console': {
151+
'class': 'logging.StreamHandler',
152+
},
129153
},
130-
'django.security.DisallowedHost': {
131-
'level': 'ERROR',
132-
'handlers': ['console', 'sentry'],
133-
'propagate': False,
154+
'loggers': {
155+
'django': {
156+
'handlers': ['console'],
157+
'level': env('DJANGO_LOG_LEVEL', default='INFO'),
158+
},
134159
},
135-
},
136-
}
137-
SENTRY_CELERY_LOGLEVEL = env.int('DJANGO_SENTRY_LOG_LEVEL', logging.INFO)
138-
RAVEN_CONFIG = {
139-
'CELERY_LOGLEVEL': env.int('DJANGO_SENTRY_LOG_LEVEL', logging.INFO),
140-
'DSN': SENTRY_DSN
141-
}
160+
}
142161

143162
# Custom Admin URL, use {% url 'admin:index' %}
144163
ADMIN_URL = env('DJANGO_ADMIN_URL')

project/config/wsgi.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
1616
"""
1717
import os
18+
import environ
19+
ROOT_DIR = environ.Path(__file__) - 2 # (/a/b/myfile.py - 3 = /)
20+
env = environ.Env()
21+
# If the project root contains a .env file, read it
22+
if os.path.isfile(str(ROOT_DIR + '.env')):
23+
environ.Env.read_env(str(ROOT_DIR + '.env'))
24+
1825

1926
from django.core.wsgi import get_wsgi_application
2027
from whitenoise.django import DjangoWhiteNoise
2128

22-
if os.environ.get("DJANGO_SETTINGS_MODULE") == "config.settings.production":
29+
if env.bool('USE_SENTRY', False):
2330
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
2431

2532
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
@@ -36,7 +43,7 @@
3643
# Use Whitenoise to serve static files
3744
# See: https://whitenoise.readthedocs.org/
3845
application = DjangoWhiteNoise(application)
39-
if os.environ.get("DJANGO_SETTINGS_MODULE") == "config.settings.production":
46+
if env.bool('USE_SENTRY', False):
4047
application = Sentry(application)
4148

4249
# Apply WSGI middleware here.

project/env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ TRANSACTION_CALLBACKS_HANDLER=examples.handlers.TransactionHandler
55

66
DJANGO_ADMIN_URL=admin/
77
DJANGO_SETTINGS_MODULE=config.settings.production
8+
DJANGO_LOG_LEVEL=INFO
89
DJANGO_SECRET_KEY=CHANGEME!!!6gihlkt1lhmkpf8^n_%4m0)zljqgg=!bh&uaq*5qogl8^-wuay
910
DJANGO_ALLOWED_HOSTS=.example.com
1011
DJANGO_SERVER_EMAIL=
@@ -18,3 +19,6 @@ MEMBEREXAMPLE_MAILMAN_SUBSCRIBE="[email protected]"
1819

1920
HOLVI_POOL=yourpoolid
2021
HOLVI_APIKEY=yourapikey
22+
23+
USE_SENTRY=True
24+
DJANGO_SENTRY_DSN=http://example.com/

0 commit comments

Comments
 (0)