spatialdev/node_rollbar

Name: node_rollbar

Owner: SpatialDev

Description: A node.js client for the Rollbar error tracking service.

Forked from: rollbar/node_rollbar

Created: 2016-02-11 23:34:24.0

Updated: 2016-02-11 23:34:25.0

Pushed: 2016-02-09 18:49:47.0

Homepage: https://rollbar.com/docs/notifier/node_rollbar/

Size: 225

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Rollbar notifier for Node.js Build Status

Node.js library for reporting exceptions and other messages to Rollbar. Requires a Rollbar account.

Quick start
nclude and initialize the rollbar library with your access token
rollbar = require("rollbar");
bar.init("POST_SERVER_ITEM_ACCESS_TOKEN");

ecord a generic message and send to rollbar
bar.reportMessage("Hello world!");

ore is required to automatically detect and report errors.
eep reading for details.

Be sure to replace `POST_SERVER_ITEM_ACCESS_TOKENwith your project's ``post_server_item``` access token, which you can find in the Rollbar.com interface.

Installation

Install using the node package manager, npm:

$ npm install --save rollbar
Configuration
Using Express
express = require('express');
rollbar = require('rollbar');

app = express();

get('/', function(req, res) {
 ...


se the rollbar error handler to send exceptions to your rollbar account
use(rollbar.errorHandler('POST_SERVER_ITEM_ACCESS_TOKEN'));

listen(6943);
Standalone

In your main application, require and initialize using your access_token::

rollbar = require("rollbar");
bar.init("POST_SERVER_ITEM_ACCESS_TOKEN");

Other options can be passed into the init() function using a second parameter. E.g.:

onfigure the library to send errors to api.rollbar.com
bar.init("POST_SERVER_ITEM_ACCESS_TOKEN", {
vironment: "staging",
dpoint: "https://api.rollbar.com/api/1/"

Usage
Uncaught exceptions

Rollbar can be registered as a handler for any uncaught exceptions in your Node process:

options = {
 Call process.exit(1) when an uncaught exception occurs but after reporting all
 pending errors to Rollbar.

 Default: false
itOnUncaughtException: true

bar.handleUncaughtExceptions("POST_SERVER_ITEM_ACCESS_TOKEN", options);
Caught exceptions

To report an exception that you have caught, use handleError or the full-powered handleErrorWithPayloadData:

rollbar = require('rollbar');
bar.init('POST_SERVER_ITEM_ACCESS_TOKEN');

{
meCode();
tch (e) {
llbar.handleError(e);

 if you have a request object (or a function that returns one), pass it as the second arg
 see below for details about what the request object is expected to be
llbar.handleError(e, request);

 you can also pass a callback, which will be called upon success/failure
llbar.handleError(e, function(err2) {
if (err2) {
  // an error occurred
} else {
  // success
}
;

 if you have a request and a callback, pass the callback last
llbar.handleError(e, request, callback);

 to specify payload options - like extra data, or the level - use handleErrorWithPayloadData
llbar.handleErrorWithPayloadData(e, {level: "warning", custom: {someKey: "arbitrary value"}});

 can also take request and callback, like handleError:
llbar.handleErrorWithPayloadData(e, {level: "info"}, request);
llbar.handleErrorWithPayloadData(e, {level: "info"}, callback);
llbar.handleErrorWithPayloadData(e, {level: "info"}, request, callback);

Log messages

To report a string message, possibly along with additional context, use reportMessage or the full-powered reportMessageWithPayloadData.

rollbar = require('rollbar');
bar.init('POST_SERVER_ITEM_ACCESS_TOKEN');

eports a string message at the default severity level ("error")
bar.reportMessage("Timeout connecting to database");


eports a string message at the level "warning", along with a request and callback
nly the first param is required
alid severity levels: "critical", "error", "warning", "info", "debug"
bar.reportMessage("Response time exceeded threshold of 1s", "warning", request, callback);

eports a string message along with additional data conforming to the Rollbar API Schema
ocumented here: https://rollbar.com/docs/api/items_post/
nly the first two params are required
bar.reportMessageWithPayloadData("Response time exceeded threshold of 1s", {
level: "warning",
custom: {
  threshold: 1,
  timeElapsed: 2.3
}
 request, callback);
The Request Object

If your Node.js application is responding to web requests, you can send data about the current request along with each report to Rollbar. This will allow you to replay requests, track events by browser, IP address, and much more.

handleError, reportMessage, handleErrorWithPayloadData, and reportMessageWithPayloadData all accept a request parameter as the second, third, third, and third arguments respectively. If it is a function, it will be called and the result used.

If you're using Express, just pass the express request object. If you're using something custom, pass an object with these keys (all optional):

Sensitive param names will be scrubbed from the request body and, if scrubHeaders is configured, headers. See the scrubFields and scrubHeaders configuration options for details.

Person Tracking

If your application has authenticated users, you can track which user (“person” in Rollbar parlance) was associated with each event.

If you're using the Passport authentication library, this will happen automatically when you pass the request object (which will have “user” attached). Otherwise, attach one of these keys to the request object described in the previous section:

Note: in Rollbar, the id is used to uniquely identify a person; email and username are supplemental and will be overwritten whenever a new value is received for an existing id. The id is a string up to 40 characters long.

Configuration reference

rollbar.init("access token", optionsObj) takes the following configuration options:

branch
The branch in your version control system for this code. e.g. `'master'`
codeVersion
The version or revision of your code. e.g. `'868ff435d6a480929103452e5ebe8671c5c89f77'`
endpoint
The rollbar API base url. Default: `'https://api.rollbar.com/api/1/'`
environment
The environment the code is running in, e.g. "production" Default: `'unspecified'`
host
The hostname of the server the node.js process is running on. Default: hostname returned from `os.hostname()`
root
The path to your code, (not including any trailing slash) which will be used to link source files on Rollbar. e.g. `'/Users/bob/Development'`
scrubFields
List of field names to scrub out of the request body (POST params). Values will be replaced with asterisks. If overriding, make sure to list all fields you want to scrub, not just fields you want to add to the default. Param names are converted to lowercase before comparing against the scrub list. Default: `['passwd', 'password', 'secret', 'confirm_password', 'password_confirmation']`
scrubHeaders
List of header names to scrub out of the request headers. Works like scrubFields. Default: `[]`
verbose
Sets whether or not to log extra info/debug messages Default: `true`
enabled
Sets whether reporting of errors to Rollbar is enabled Default: `true`
Examples

See the examples directory for more use cases.

Sails.js

For a full example of setting up a new Sails.js project with Rollbar integration see rollbar-sailsjs-example.

Help / Support

If you have any questions, feedback, etc., drop us a line at support@rollbar.com

For bug reports, please open an issue on GitHub.

Contributing

The project is hosted on GitHub. If you'd like to contribute a change:

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

We're using vows for testing. To run the tests, run: vows --spec test/*


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.