Getting Started
Copy-paste the following code into the <head> of every page you want to track. It should be as high as possible, before any other <script> tags.
If you're running Rollbar on an environment besides production, change the server.environment value to something else (e.g. "staging").
See below for additional configuration options.
Test your installation
- Navigate your browser to a page that has the above code installed
- Type the following code into the address bar (not the console) and press enter:
javascript:testing_rollbar_123();
As long as you don't happen to have a function by that name, this will cause an uncaught error that will be reported to Rollbar. It should appear in the dashboard within a few seconds.
Generic logging
In addition to catching top-level errors, you can use _rollbar.push to send custom log messages. It is fully-asynchronous and safe to call anywhere in your code after the <script> tag above.
_rollbar.push accepts arguments of any of the following forms:
-
An
Errorinstance (i.e. for reporting a caught exception):
for (var i = 0; i < someVar; ++i) { var elem = i; var callback = function(err) { if (err !== null) { console.log('An error occurred while reporting elem ' + elem + ' to Rollbar, ' + err); } } try { doSomething(); } catch (e) { _rollbar.push(e, callback); } }
-
A plain string:
_rollbar.push("Some log message");
-
An object containing a 'msg' property, an optional 'level' property, an optional '_fingerprint' property, and any arbitrary data you want to send (as long as it can be encoded by JSON.stringify()):
Note: jQuery objects can not be encoded by JSON.stringify(), so don't pass them as-is._rollbar.push({level: 'warning', msg: "Some warning message", point: {x: 5, y: 10}});
levelis optional and can take any of the following values: 'critical', 'error', 'warning', 'info', 'debug'
_fingerprintis optional; if provided, it will be override the fingerprint used for grouping. Should be a string no longer than 40 characters.
If you want to wrap console.log, try the following:
if (typeof console === 'undefined') {
console = {log: function() {}};
}
var old_console_log = console.log;
function rollbar_console_log() {
old_console_log.apply(this, arguments);
var message = {level: 'info', msg: arguments[0]};
for (var i = 1; i < arguments.length; i++) {
message[i] = arguments[i];
}
_rollbar.push(message);
}
console.log = rollbar_console_log;
Configuration
The default configuration sets:
- the environment name to "production"
- the severity level of all uncaught errors to "warning"
- a client-side rate-limit of 5 per minute
All of these are configurable via the _rollbarParams object.
- checkIgnore
- An optional function that will be used to ignore uncaught exceptions based on its return value. The function signature should be:
function checkIgnore(errMsg, url, lineNo) { ... }and should returntrueif the error should be ignored.
Default:null - context
- Name of the page context -- i.e. route name, url, etc. Can be used in the Rollbar interface to search for items by context prefix.
- itemsPerMinute
- Max number of items to report per minute. The limit counts uncaught errors (reported through window.onerror) and any direct calls to
_rollbar.push(). This is intended as a sanity check against infinite loops, but if you're using Rollbar heavily for logging, you may want to increase this.
Default: 5 - level
- The severity level to report javascript errors at. One of
"critical","error","warning","info","debug".
Default:"warning" - person
- An object identifying the logged-in user, containing an
id(required), and optionally ausernameandemail(all strings). Passing this will allow you to see which users were affected by particular errors, as well as all the errors that a particular user experienced. - server.branch
- The name of the branch of the code that is running. Used for linking filenames in stacktraces to GitHub.
Default:master. - server.environment
- Environment name, e.g.
"production"or"development". Can be an arbitrary string, though to take advantage of the default notifications settings, we recommend using"production"for your production environment. - server.host
- The hostname of the machine that rendered the page, e.g.
web1.mysite.com. e.g. in Python, usesocket.gethostname().
Example
Using all config options:
var _rollbarParams = {
checkIgnore: function(errMsg, url, lineNo) {
// don't ignore anything (default)
return false;
},
context: "home#index",
itemsPerMinute: 60,
level: "error",
person: {
id: 12345,
username: "johndoe",
email: "johndoe@example.com"
},
"server.branch": "develop",
"server.environment": "staging",
"server.host": "web1"
};
Callbacks
You can pass in an optional callback to _rollbar.push(obj, callback) which will be called when the item is reported to the Rollbar servers. If an error occurs, the callback will be evaluated with 1 argument defining the error that occurred.
for (var i = 0; i < someVar; ++i) {
var elem = i;
var callback = function(err) {
if (err !== null) {
console.log('An error occurred while reporting elem ' + elem + ' to Rollbar, ' + err);
}
}
try {
doSomething();
} catch (e) {
_rollbar.push(e, callback);
}
}