Submit Blog  RSS Feeds

Monday, September 24, 2012

Logging JavaScript exceptions using raven-js and sentry.

Let's face it, its hard to obtain 100% unit-test code coverage, likewise it's nearly impossible to implement functional tests for all possible scenarios. A good way of recognizing problems and locating them is logging exceptions and other informative messages. For small projects logging to std out/err or files is fine, but if there are hundreds of people exploiting your application every minute you should think of a more scalable solution.

Sentry is a good option, it's a real-time event logging platform that may be set-up as a standalone application. Other pros are:
  • support of different levels of logging / dynamic filtering
  • logging data from many independent projects
  • presistent log storing (database)
  • user privilege configuration / email notification
  • there are clients for many popular programming languages
  • it's based on python/django
  • it's based on python/django
  • it's based on python/django
  • ....
I can go on like this forever :-) You can easily install sentry via pip.

~ virtualenv --no-site-packages sentry_env && cd sentry_env
~ source bin/activate
(sentry_env) ~ pip install sentry
(sentry_env) ~ sentry init

Now all you have to configure database access and other important parameters, check out the sentry configuration guide for more information.

But let's get back to the main thought. Among programming languages that have clients for sentry there is also JavaScript. This wouldn't be a surprise if not for the fact that (besides node.js) JS is usually executed on the client side (web browser). Raven-js (don't confuse it the client for RavenDB) can log messages / catch exceptions and send them via AJAX requests to your sentry application. In order to set up the logging script you should first configure sentry and create a project and obtain generated project public key. Then use the following code:




Setting up raven-js and observing how your scripts crash on IE is just priceless :-)

~KR


2 comments:

  1. Raven 1.0 automatically registers an onerror handler. The Raven.process method has been removed.

    ReplyDelete
  2. whoah this weblog is excellent i like reading your articles.
    Keep up the good work! You realize, lots of persons are hunting round for this info, you could help them greatly.


    Also visit my web blog; provillus

    ReplyDelete

free counters