Browse Source

Make the example app more functional and demonstrate various ways of
sending data to Sentry.

Marc Abramowitz 13 years ago
parent
commit
334080fdb5

+ 21 - 2
example/README.rst

@@ -1,3 +1,22 @@
-To run this app you will need raven client::
+This is a sample Django app that illustrates various ways of sending data to Sentry.
 
-  pip install raven
+To run this app you will need raven client installed::
+
+    pip install raven
+
+Edit :file:`settings.py` and change `SENTRY_DSN` so that it matches your Sentry server.
+
+Then do::
+
+    python manage.py syncdb
+    python manage.py runserver
+
+And visit these URLS:
+
+- http://localhost:8000/captureMessage/
+- http://localhost:8000/captureException/
+- http://localhost:8000/loggingError/
+- http://localhost:8000/page_no_exist/
+
+For more information, see the `Configuring Django section of the Raven
+documentation <http://raven.readthedocs.org/en/latest/config/django.html>`_.

+ 22 - 2
example/settings.py

@@ -1,5 +1,9 @@
 # Django settings for example project.
 
+import os
+
+PROJECT_ROOT = os.path.dirname(__file__)
+
 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
 
@@ -93,6 +97,8 @@ TEMPLATE_LOADERS = (
 )
 
 MIDDLEWARE_CLASSES = (
+    'raven.contrib.django.middleware.SentryResponseErrorIdMiddleware',
+    'raven.contrib.django.middleware.Sentry404CatchMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
@@ -107,6 +113,7 @@ TEMPLATE_DIRS = (
     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.
+    os.path.join(PROJECT_ROOT, 'templates'),
 )
 
 INSTALLED_APPS = (
@@ -124,6 +131,11 @@ INSTALLED_APPS = (
     'raven.contrib.django',
 )
 
+# DSN of your Sentry server (https://github.com/dcramer/sentry)
+# For info on configuring Django to use Sentry, see
+# http://raven.readthedocs.org/en/latest/config/django.html
+SENTRY_DSN = 'http://public:secret@example.com/1'
+
 # A sample logging configuration. The only tangible logging
 # performed by this configuration is to send an email to
 # the site admins on every HTTP 500 error.
@@ -131,11 +143,19 @@ INSTALLED_APPS = (
 # more details on how to customize your logging configuration.
 LOGGING = {
     'version': 1,
-    'disable_existing_loggers': False,
+    'disable_existing_loggers': True,
+    'root': {
+        'level': 'WARNING',
+        'handlers': ['sentry'],
+    },
     'handlers': {
+        'sentry': {
+            'level': 'WARNING',
+            'class': 'raven.contrib.django.handlers.SentryHandler',
+        },
         'mail_admins': {
             'level': 'ERROR',
-            'class': 'django.utils.log.AdminEmailHandler'
+            'class': 'django.utils.log.AdminEmailHandler',
         }
     },
     'loggers': {

+ 5 - 0
example/templates/captureException.html

@@ -0,0 +1,5 @@
+{% extends "layout.html" %}
+
+{% block main %}
+    <div class="alert alert-error">I just sent an exception to sentry with id {{ message_id }}.</div>
+{% endblock %}

+ 5 - 0
example/templates/captureMessage.html

@@ -0,0 +1,5 @@
+{% extends "layout.html" %}
+
+{% block main %}
+    <div class="alert alert-notice">I just sent a message to sentry with id {{ message_id }}.</div>
+{% endblock %}

+ 45 - 0
example/templates/layout.html

@@ -0,0 +1,45 @@
+{% load i18n %}
+{% load sentry_helpers %}
+
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta http-equiv="content-type" content="text/html; charset=utf-8">
+        <meta name="robots" content="NONE,NOARCHIVE">
+        <link href="{% url sentry-media "styles/global.min.css" %}" rel="stylesheet" type="text/css"/>
+        <title>{% block title %}Sentry{% endblock %}</title>
+        {% block meta %}
+        {% endblock %}
+    </head>
+
+    <body>
+        {% block body %}
+        {% block header %}
+        {% endblock %}
+        <section id="content" class="{% block bodyclass %}with-sidebar{% endblock %}">
+            <div class="container">
+                <div class="content">
+                    {% block content_before %}
+                    {% endblock %}
+                    {% block content %}
+                        <div class="main">
+                            {% block main %}
+                            {% endblock %}
+                        </div>
+                    {% endblock %}
+                    {% block content_after %}
+                    {% endblock %}
+                    </div>
+                </div>
+            </div>
+        </section>
+        <footer>
+            <div class="container">
+                {% block footer %}
+                    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>
+                {% endblock %}
+            </div>
+        </footer>
+        {% endblock %}
+    </body>
+</html>

+ 5 - 0
example/templates/loggingError.html

@@ -0,0 +1,5 @@
+{% extends "layout.html" %}
+
+{% block main %}
+    <div class="alert alert-notice">I just logged a message with id {{ request.sentry.id }}.</div>
+{% endblock %}

+ 3 - 0
example/urls.py

@@ -3,4 +3,7 @@ from django.conf.urls.defaults import patterns, include, url
 
 urlpatterns = patterns('',
     (r'^sentry/', include('sentry.web.urls')),
+    (r'^captureMessage/', 'views.captureMessage'),
+    (r'^captureException/', 'views.captureException'),
+    (r'^loggingError/', 'views.loggingError'),
 )

+ 26 - 0
example/views.py

@@ -0,0 +1,26 @@
+import logging
+from django.http import HttpResponse
+from django.shortcuts import render
+from raven.contrib.django.models import get_client
+
+client = get_client()
+logger = logging.getLogger(__file__)
+
+def captureMessage(request):
+    message_id = client.captureMessage("This is a message from the example Django app")
+    return render(request, 'captureMessage.html', {"message_id": message_id})
+
+def captureException(request):
+    try:
+        raise RuntimeError("This is an exception from the example Django app.")
+    except RuntimeError:
+        message_id = client.captureException()
+
+    return render(request, 'captureException.html', {"message_id": message_id})
+
+def loggingError(request):
+    logger.error('This error was sent to a logger', exc_info=True, extra={
+        # Optionally pass a request and we'll grab any information we can
+        'request': request,
+    })
+    return render(request, 'loggingError.html', {"request": request})