README.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. --------------
  2. django-sentry
  3. --------------
  4. Sentry provides you with a generic interface to view and interact with your error logs. By
  5. default, it will catch any exception thrown by Django and store it in a database. With this
  6. it allows you to interact and view near real-time information to discover issues and more
  7. easily trace them in your application.
  8. * Note: Sentry has only been tested under a PostgreSQL environment.
  9. ==========
  10. Screenshot
  11. ==========
  12. .. image:: http://dl.dropbox.com/u/116385/sentry2.jpg
  13. ============
  14. Requirements
  15. ============
  16. - **Django >= 1.2** (to use a secondary database to store error logs)
  17. - **django-indexer** (stores metadata indexes)
  18. - **django-paging**
  19. - **pygooglechart** (to generate *optional* error reports)
  20. =========
  21. Upgrading
  22. =========
  23. If you use South migrations, simply run::
  24. python manage.py migrate sentry
  25. If you don't use South, then start.
  26. =======
  27. Install
  28. =======
  29. The easiest way to install the package is via pip::
  30. pip install django-sentry --upgrade
  31. OR, if you're not quite on the same page (work on that), with setuptools::
  32. easy_install django-sentry
  33. Once installed, update your settings.py and add ``sentry``, ``sentry.client``, ``indexer``, and ``paging`` to ``INSTALLED_APPS``::
  34. INSTALLED_APPS = (
  35. 'django.contrib.admin',
  36. 'django.contrib.auth',
  37. 'django.contrib.contenttypes',
  38. 'django.contrib.sessions',
  39. # don't forget to add the dependancies!
  40. 'indexer',
  41. 'paging',
  42. 'sentry',
  43. 'sentry.client',
  44. ...
  45. )
  46. Finally, run ``python manage.py syncdb`` to create the database tables.
  47. (If you use South, you'll need to use ``python manage.py migrate sentry``)
  48. ==========================
  49. Multi-server configuration
  50. ==========================
  51. To configure Sentry for use in a multi-server environment, first you'll want to configure your Sentry server (not your application)::
  52. INSTALLED_APPS = [
  53. ...
  54. 'indexer',
  55. 'paging',
  56. 'sentry',
  57. 'sentry.client',
  58. ]
  59. SENTRY_KEY = '0123456789abcde'
  60. And on each of your application servers, specify the URL of the Sentry server, add ``sentry.client`` to ``INSTALLED_APPS``, and specify the same key used in your Sentry server's settings::
  61. # This should be the absolute URI of sentries store view
  62. SENTRY_REMOTE_URL = 'http://your.sentry.server/sentry/store/'
  63. INSTALLED_APPS = [
  64. ...
  65. 'sentry.client',
  66. ]
  67. SENTRY_KEY = '0123456789abcde'
  68. You may also specify an alternative timeout to the default (which is 5 seconds) for all outgoing logging requests::
  69. SENTRY_REMOTE_TIMEOUT = 5
  70. Sentry also allows you to support high availability by pushing to multiple servers::
  71. SENTRY_REMOTE_URL = ['http://server1/sentry/store', 'http://server2/sentry/store']
  72. ===========================
  73. Other configuration options
  74. ===========================
  75. Several options exist to configure django-sentry via your ``settings.py``:
  76. #############
  77. SENTRY_ADMINS
  78. #############
  79. On smaller sites you may wish to enable throttled emails, we recommend doing this by first
  80. removing the ``ADMINS`` setting in Django, and adding in ``SENTRY_ADMINS``::
  81. ADMINS = ()
  82. SENTRY_ADMINS = ('root@localhost',)
  83. This will send out a notification the first time an error is seen, and the first time an error is
  84. seen after it has been resolved.
  85. #######################
  86. SENTRY_CATCH_404_ERRORS
  87. #######################
  88. Enable catching of 404 errors in the logs. Default value is ``False``::
  89. SENTRY_CATCH_404_ERRORS = True
  90. You can skip other custom exception types by adding a ``skip_sentry = True`` attribute to them.
  91. #####################
  92. SENTRY_DATABASE_USING
  93. #####################
  94. Use a secondary database to store error logs. This is useful if you have several websites and want to aggregate error logs onto one database server::
  95. # This should correspond to a key in your DATABASES setting
  96. SENTRY_DATABASE_USING = 'default'
  97. You should also enable the ``SentryRouter`` to avoid things like extraneous table creation::
  98. DATABASE_ROUTERS = [
  99. 'sentry.routers.SentryRouter',
  100. ...
  101. ]
  102. .. note:: This functionality REQUIRES Django 1.2.
  103. ############################
  104. Integration with ``logging``
  105. ############################
  106. django-sentry supports the ability to directly tie into the ``logging`` module. To use it simply add ``SentryHandler`` to your logger::
  107. import logging
  108. from sentry.client.handlers import SentryHandler
  109. logging.getLogger().addHandler(SentryHandler())
  110. # Add StreamHandler to sentry's default so you can catch missed exceptions
  111. logging.getLogger('sentry').addHandler(logging.StreamHandler())
  112. You can also use the ``exc_info`` and ``extra=dict(url=foo)`` arguments on your ``log`` methods. This will store the appropriate information and allow django-sentry to render it based on that information:
  113. logging.error('There was some crazy error', exc_info=sys.exc_info(), extra={'url': request.build_absolute_uri()})
  114. ##############
  115. SENTRY_TESTING
  116. ##############
  117. Enabling this setting allows the testing of Sentry exception handler even if Django DEBUG is enabled.
  118. Default value is ``False``
  119. .. note:: Normally when Django DEBUG is enabled the Sentry exception handler is immediately skipped
  120. ###########
  121. SENTRY_NAME
  122. ###########
  123. This will override the ``server_name`` value for this installation. Defaults to ``socket.get_hostname()``.
  124. =====
  125. Usage
  126. =====
  127. Set up a viewer server (or use your existing application server) and add sentry to your INSTALLED_APPS and your included URLs::
  128. # urls.py
  129. urlpatterns = patterns('',
  130. (r'^admin/', include(admin.site.urls)),
  131. (r'^sentry/', include('sentry.urls')),
  132. )
  133. Now enjoy your beautiful new error tracking at ``/sentry/``.
  134. ===
  135. API
  136. ===
  137. For the technical, here's some further docs:
  138. If you wish to access these within your own views and models, you may do so via the standard model API::
  139. from sentry.models import Message, GroupedMessage
  140. # Pull the last 10 unresolved errors.
  141. GroupedMessage.objects.filter(status=0).order_by('-last_seen')[0:10]
  142. You can also record errors outside of handler if you want::
  143. from sentry.client.base import SentryClient
  144. try:
  145. ...
  146. except Exception, exc:
  147. SentryClient.create_from_exception([url=None, view=None])
  148. If you wish to log normal messages (useful for non-``logging`` integration)::
  149. from sentry.client.base import SentryClient
  150. import logging
  151. SentryClient.create_from_text('Message Message'[, level=logging.WARNING, url=None])
  152. Both the ``url`` and ``level`` parameters are optional. ``level`` should be one of the following:
  153. * ``logging.DEBUG``
  154. * ``logging.INFO``
  155. * ``logging.WARNING``
  156. * ``logging.ERROR``
  157. * ``logging.FATAL``
  158. If you have a custom exception class, similar to Http404, or something else you don't want to log,
  159. you can also add ``skip_sentry = True`` to your exception class or instance, and sentry will simply ignore
  160. the error.
  161. =====
  162. Notes
  163. =====
  164. * sentry-client will automatically integrate with django-idmapper.
  165. * sentry-client supports South migrations.
  166. * The fact that the admin shows large quantities of results, even if there aren't, is not a bug. This is an efficiency hack on top of Django.