Commit | Line | Data |
---|---|---|
3fbba1af JW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. | |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | import os | |
18 | import logging | |
19 | ||
20 | from mediagoblin.tools import pluginapi | |
3fbba1af JW |
21 | |
22 | _log = logging.getLogger(__name__) | |
23 | ||
3fbba1af | 24 | |
f3f53028 | 25 | def get_client(): |
40ef3f5e | 26 | from raven import Client |
0c1ae351 | 27 | config = pluginapi.get_config('mediagoblin.plugins.raven') |
3fbba1af | 28 | |
3fbba1af JW |
29 | sentry_dsn = config.get('sentry_dsn') |
30 | ||
f3f53028 JW |
31 | client = None |
32 | ||
3fbba1af JW |
33 | if sentry_dsn: |
34 | _log.info('Setting up raven from plugin config: {0}'.format( | |
35 | sentry_dsn)) | |
32174bed | 36 | client = Client(sentry_dsn) |
3fbba1af JW |
37 | elif os.environ.get('SENTRY_DSN'): |
38 | _log.info('Setting up raven from SENTRY_DSN environment variable: {0}'\ | |
39 | .format(os.environ.get('SENTRY_DSN'))) | |
40 | client = Client() # Implicitly looks for SENTRY_DSN | |
f3f53028 JW |
41 | |
42 | if not client: | |
3fbba1af | 43 | _log.error('Could not set up client, missing sentry DSN') |
f3f53028 JW |
44 | return None |
45 | ||
46 | return client | |
47 | ||
48 | ||
49 | def setup_celery(): | |
50 | from raven.contrib.celery import register_signal | |
51 | ||
52 | client = get_client() | |
b9c5c972 JW |
53 | |
54 | register_signal(client) | |
55 | ||
3fbba1af | 56 | |
f3f53028 JW |
57 | def setup_logging(): |
58 | config = pluginapi.get_config('mediagoblin.plugins.raven') | |
59 | ||
60 | conf_setup_logging = False | |
61 | if config.get('setup_logging'): | |
62 | conf_setup_logging = bool(int(config.get('setup_logging'))) | |
63 | ||
64 | if not conf_setup_logging: | |
65 | return | |
66 | ||
67 | from raven.handlers.logging import SentryHandler | |
68 | from raven.conf import setup_logging | |
69 | ||
70 | client = get_client() | |
71 | ||
72 | _log.info('Setting up raven logging handler') | |
73 | ||
74 | setup_logging(SentryHandler(client)) | |
75 | ||
76 | ||
77 | def wrap_wsgi(app): | |
78 | from raven.middleware import Sentry | |
79 | ||
80 | client = get_client() | |
81 | ||
82 | _log.info('Attaching raven middleware...') | |
83 | ||
84 | return Sentry(app, client) | |
85 | ||
86 | ||
3fbba1af | 87 | hooks = { |
f3f53028 JW |
88 | 'setup': setup_logging, |
89 | 'wrap_wsgi': wrap_wsgi, | |
90 | 'celery_logging_setup': setup_logging, | |
91 | 'celery_setup': setup_celery, | |
3fbba1af | 92 | } |