Skip to content

Commit 334080f

Browse files
committed
Make the example app more functional and demonstrate various ways of
sending data to Sentry.
1 parent ddc1296 commit 334080f

File tree

8 files changed

+132
-4
lines changed

8 files changed

+132
-4
lines changed

example/README.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
To run this app you will need raven client::
1+
This is a sample Django app that illustrates various ways of sending data to Sentry.
22

3-
pip install raven
3+
To run this app you will need raven client installed::
4+
5+
pip install raven
6+
7+
Edit :file:`settings.py` and change `SENTRY_DSN` so that it matches your Sentry server.
8+
9+
Then do::
10+
11+
python manage.py syncdb
12+
python manage.py runserver
13+
14+
And visit these URLS:
15+
16+
- http://localhost:8000/captureMessage/
17+
- http://localhost:8000/captureException/
18+
- http://localhost:8000/loggingError/
19+
- http://localhost:8000/page_no_exist/
20+
21+
For more information, see the `Configuring Django section of the Raven
22+
documentation <http://raven.readthedocs.org/en/latest/config/django.html>`_.

example/settings.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Django settings for example project.
22

3+
import os
4+
5+
PROJECT_ROOT = os.path.dirname(__file__)
6+
37
DEBUG = True
48
TEMPLATE_DEBUG = DEBUG
59

@@ -93,6 +97,8 @@
9397
)
9498

9599
MIDDLEWARE_CLASSES = (
100+
'raven.contrib.django.middleware.SentryResponseErrorIdMiddleware',
101+
'raven.contrib.django.middleware.Sentry404CatchMiddleware',
96102
'django.middleware.common.CommonMiddleware',
97103
'django.contrib.sessions.middleware.SessionMiddleware',
98104
'django.middleware.csrf.CsrfViewMiddleware',
@@ -107,6 +113,7 @@
107113
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
108114
# Always use forward slashes, even on Windows.
109115
# Don't forget to use absolute paths, not relative paths.
116+
os.path.join(PROJECT_ROOT, 'templates'),
110117
)
111118

112119
INSTALLED_APPS = (
@@ -124,18 +131,31 @@
124131
'raven.contrib.django',
125132
)
126133

134+
# DSN of your Sentry server (https://github.com/dcramer/sentry)
135+
# For info on configuring Django to use Sentry, see
136+
# http://raven.readthedocs.org/en/latest/config/django.html
137+
SENTRY_DSN = 'http://public:[email protected]/1'
138+
127139
# A sample logging configuration. The only tangible logging
128140
# performed by this configuration is to send an email to
129141
# the site admins on every HTTP 500 error.
130142
# See http://docs.djangoproject.com/en/dev/topics/logging for
131143
# more details on how to customize your logging configuration.
132144
LOGGING = {
133145
'version': 1,
134-
'disable_existing_loggers': False,
146+
'disable_existing_loggers': True,
147+
'root': {
148+
'level': 'WARNING',
149+
'handlers': ['sentry'],
150+
},
135151
'handlers': {
152+
'sentry': {
153+
'level': 'WARNING',
154+
'class': 'raven.contrib.django.handlers.SentryHandler',
155+
},
136156
'mail_admins': {
137157
'level': 'ERROR',
138-
'class': 'django.utils.log.AdminEmailHandler'
158+
'class': 'django.utils.log.AdminEmailHandler',
139159
}
140160
},
141161
'loggers': {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends "layout.html" %}
2+
3+
{% block main %}
4+
<div class="alert alert-error">I just sent an exception to sentry with id {{ message_id }}.</div>
5+
{% endblock %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends "layout.html" %}
2+
3+
{% block main %}
4+
<div class="alert alert-notice">I just sent a message to sentry with id {{ message_id }}.</div>
5+
{% endblock %}

example/templates/layout.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{% load i18n %}
2+
{% load sentry_helpers %}
3+
4+
<!DOCTYPE html>
5+
<html lang="en">
6+
<head>
7+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
8+
<meta name="robots" content="NONE,NOARCHIVE">
9+
<link href="{% url sentry-media "styles/global.min.css" %}" rel="stylesheet" type="text/css"/>
10+
<title>{% block title %}Sentry{% endblock %}</title>
11+
{% block meta %}
12+
{% endblock %}
13+
</head>
14+
15+
<body>
16+
{% block body %}
17+
{% block header %}
18+
{% endblock %}
19+
<section id="content" class="{% block bodyclass %}with-sidebar{% endblock %}">
20+
<div class="container">
21+
<div class="content">
22+
{% block content_before %}
23+
{% endblock %}
24+
{% block content %}
25+
<div class="main">
26+
{% block main %}
27+
{% endblock %}
28+
</div>
29+
{% endblock %}
30+
{% block content_after %}
31+
{% endblock %}
32+
</div>
33+
</div>
34+
</div>
35+
</section>
36+
<footer>
37+
<div class="container">
38+
{% block footer %}
39+
Sentry {% sentry_version %} | Conjured up by the <a href="http://code.disqus.com">DISQUS</a> team and other noble <a href="https://github.com/dcramer/sentry/contributors">sorcerers</a>. | Sentry is <a href="https://github.com/dcramer/sentry">Open Source Software</a>
40+
{% endblock %}
41+
</div>
42+
</footer>
43+
{% endblock %}
44+
</body>
45+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends "layout.html" %}
2+
3+
{% block main %}
4+
<div class="alert alert-notice">I just logged a message with id {{ request.sentry.id }}.</div>
5+
{% endblock %}

example/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33

44
urlpatterns = patterns('',
55
(r'^sentry/', include('sentry.web.urls')),
6+
(r'^captureMessage/', 'views.captureMessage'),
7+
(r'^captureException/', 'views.captureException'),
8+
(r'^loggingError/', 'views.loggingError'),
69
)

example/views.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import logging
2+
from django.http import HttpResponse
3+
from django.shortcuts import render
4+
from raven.contrib.django.models import get_client
5+
6+
client = get_client()
7+
logger = logging.getLogger(__file__)
8+
9+
def captureMessage(request):
10+
message_id = client.captureMessage("This is a message from the example Django app")
11+
return render(request, 'captureMessage.html', {"message_id": message_id})
12+
13+
def captureException(request):
14+
try:
15+
raise RuntimeError("This is an exception from the example Django app.")
16+
except RuntimeError:
17+
message_id = client.captureException()
18+
19+
return render(request, 'captureException.html', {"message_id": message_id})
20+
21+
def loggingError(request):
22+
logger.error('This error was sent to a logger', exc_info=True, extra={
23+
# Optionally pass a request and we'll grab any information we can
24+
'request': request,
25+
})
26+
return render(request, 'loggingError.html', {"request": request})

0 commit comments

Comments
 (0)