Giter Club home page Giter Club logo

raven-node's Introduction

Raven Build Status

Log errors and stack traces in Sentry from within your Node.js applications. Includes middleware support for Connect/Express.

All processing and sending happens asynchronously to not slow things down if/when Sentry is down or slow.

Installation

$ npm install raven

Basic Usage

var raven = require('raven');
var client = new raven.Client('{{ SENTRY_DSN }}');

client.captureMessage('Hello, world!');

Run with:

$ NODE_ENV=production node script.js

Logging an error

client.captureError(new Error('Broke!'));

Logging a query

client.captureQuery('SELECT * FROM `awesome`', 'mysql');

Sentry Identifier

client.captureMessage('Hello, world!', function(result) {
    console.log(client.getIdent(result));
});
client.captureError(new Error('Broke!'), function(result) {
  console.log(client.getIdent(result));
});

Note: client.captureMessage will also return the result directly without the need for a callback, such as: var result = client.captureMessage('Hello, world!');

Additional data

You might want to send additional data to sentry, which will help to understand the error. Therefore you should use extra property of the object passed as a second optional parameter. If you add user specific data to the message, event will not grouped by sentry.

client.captureMessage('Hello, world!', {extra: {userId: 123}}, function(result) {
    console.log(client.getIdent(result));
});

client.captureError(new Error('Broke!'), {extra: {userId: 123}});

Logging levels

You can specify a level in the second optional parameter. Default level is error. There are 5 logging levels (in order):

  • debug (the least serious)
  • info
  • warning
  • error
  • fatal (the most serious)
client.captureMessage('Hello, world!', {level: 'info'}, function(result) {
    console.log(client.getIdent(result));
});

client.captureError(new Error('Broke!'), {level: 'fatal'});

Events

If you really care if the event was logged or errored out, Client emits two events, logged and error:

client.on('logged', function(){
  console.log('Yay, it worked!');
});
client.on('error', function(){
  console.log('oh well, Sentry is broke.');
})
client.captureMessage('Boom');

Environment variables

NODE_ENV

NODE_ENV must be set to production for Sentry to actually work. Without being in production, a warning is issued and logging disabled.

SENTRY_DSN

Optionally declare the DSN to use for the client through the environment. Initializing the client in your app won't require setting the DSN.

SENTRY_NAME

Optionally set the name for the client to use. What is name?

SENTRY_SITE

Optionally set the site for the client to use. What is site?

Catching global errors

For those times when you don't catch all errors in your application. ;)

client.patchGlobal();
// or
raven.patchGlobal(client);
// or
raven.patchGlobal('{{ SENTRY_DSN }}');

Methods

new raven.Client(dsn[, options])
client.captureMessage(string[,callback])
client.captureError(Error[,callback])
client.captureQuery(string, string[,callback])

Integrations

Connect/Express middleware

The Raven middleware can be used as-is with either Connect or Express in the same way. Take note that in your middlewares, Raven must appear after your main handler to pick up any errors that may result from handling a request.

Connect

var connect = require('connect');
function mainHandler(req, res) {
  throw new Error('Broke!');
}
function onError(err, req, res, next) {
  // The error id is attached to `res.sentry` to be returned
  // and optionally displayed to the user for support.
  res.statusCode = 500;
  res.end(res.sentry+'\n');
}
connect(
  connect.bodyParser(),
  connect.cookieParser(),
  mainHandler,
  raven.middleware.connect('{{ SENTRY_DSN }}'),
  onError, // optional error handler if you want to display the error id to a user
).listen(3000);

Express

var app = require('express').createServer();
app.error(raven.middleware.express('{{ SENTRY_DSN }}'));
app.error(onError); // optional error handler if you want to display the error id to a user
app.get('/', function mainHandler(req, res) {
  throw new Error('Broke!');
});
app.listen(3000);

Todo

  • More complete test coverage
  • More comments in code
  • More third party integration

raven-node's People

Contributors

kof avatar mattrobenolt avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.