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.
Be sure to replace POST_CLIENT_ITEM_ACCESS_TOKEN
with your project's post_client_item
access token, which you can find in the Rollbar.com interface.
<script>
var _rollbarParams = {"server.environment": "production"};
_rollbarParams["notifier.snippet_version"] = "2"; var _rollbar=["POST_CLIENT_ITEM_ACCESS_TOKEN", _rollbarParams]; var _ratchet=_rollbar;
(function(w,d){w.onerror=function(e,u,l,c,err){_rollbar.push({_t:'uncaught',e:e,u:u,l:l,c:c,err:err});};var i=function(){var s=d.createElement("script");var
f=d.getElementsByTagName("script")[0];s.src="//d37gvrvc0wt4s1.cloudfront.net/js/1/rollbar.min.js";s.async=!0;
f.parentNode.insertBefore(s,f);};if(w.addEventListener){w.addEventListener("load",i,!1);}else{w.attachEvent("onload",i);}})(window,document);
</script>
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.
- 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.
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
Error
instance (i.e. for reporting a caught exception):
try {
doSomething();
} catch (e) {
_rollbar.push(e);
}
- 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())
:
_rollbar.push({level: 'warning', msg: "Some warning message", point: {x: 5, y: 10}});
- Note: jQuery objects can not be encoded by
JSON.stringify()
, so don't pass them as-is.level
is optional and can take any of the following values: 'critical', 'error', 'warning', 'info', 'debug'_fingerprint
is 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;
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: "[email protected]"
},
"server.branch": "develop",
"server.environment": "staging",
"server.host": "web1"
};
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 return ```true``` if the error should be ignored.
Default:
null
- client.javascript.code_version
- Version control number (i.e. git SHA) of the current revision. Used for linking filenames in stacktraces to Github.
- 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.
- custom
- An object containing any custom data you'd like to include with all reports. Must be JSON serializable -- note that jQuery objects are _not_ JSON serializable.
- 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 a ```username``` and ```email``` (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.Default:
"production"
- server.host
- The hostname of the machine that rendered the page
e.g.
"web1.mysite.com"
e.g. in Python, use
socket.gethostname()
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, otherwise the callback will be evaluated a null error and the reported item's uuid as the second argument.
for (var i = 0; i < someVar; ++i) {
var elem = i;
var callback = function(err, uuid) {
if (err !== null) {
console.log('An error occurred while reporting elem ' + elem + ' to Rollbar, ' + err);
}
}
try {
doSomething();
} catch (e) {
_rollbar.push(e, callback);
}
}
If you use jQuery 1.7 and up, you can include a plugin script that will instrument jQuery to wrap any functions passed into jQuery's ready(), on() and off() to catch errors and report them to Rollbar. To install this plugin, copy the following snippet into your pages, making sure it is BELOW the <script>
tag where jQuery is loaded:
<script>
!function(r,n,e){if(!n._rollbar){return}var o={"notifier.plugins.jquery.version":"0.0.4"};n._rollbar.push({_rollbarParams:o});var t=function(r){if(n.console){n.console.log(r.message+" [reported to Rollbar]")}};r(e).ajaxError(function(r,e,o,t){var u=e.status;var a=o.url;n._rollbar.push({level:"warning",msg:"jQuery ajax error for url "+a,jquery_status:u,jquery_url:a,jquery_thrown_error:t,jquery_ajax_error:true})});var u=r.fn.ready;r.fn.ready=function(r){return u.call(this,function(){try{r()}catch(e){n._rollbar.push(e);t(e)}})};var a={};var l=r.fn.on;r.fn.on=function(r,e,o,u,f){var i=function(r){var e=function(){try{return r.apply(this,arguments)}catch(e){n._rollbar.push(e);t(e);return null}};a[r]=e;return e};if(e&&typeof e==="function"){e=i(e)}else if(o&&typeof o==="function"){o=i(o)}else if(u&&typeof u==="function"){u=i(u)}return l.call(this,r,e,o,u,f)};var f=r.fn.off;r.fn.off=function(r,n,e){if(n&&typeof n==="function"){n=a[n];delete a[n]}else{e=a[e];delete a[e]}return f.call(this,r,n,e)}}(jQuery,window,document);
</script>
The plugin will also automatically report any AJAX errors using jQuery's ajaxError()
handler. You can disable this functionality by providing the following configuration option in the _rollbarParams
of your base snippet:
"notifier.plugins.jquery.ignoreAjaxErrors": true
To use Rollbar with PhoneGap, browser extensions, or any other environment where your code is loaded from a protocol besides http
or https
, use the following snippet instead. The only change is to use https
instead of a protocol-less URL.
<script>
var _rollbarParams = {"server.environment": "production"};
_rollbarParams["notifier.snippet_version"] = "2"; var _rollbar=["POST_CLIENT_ITEM_ACCESS_TOKEN", _rollbarParams]; var _ratchet=_rollbar;
(function(w,d){w.onerror=function(e,u,l,c,err){_rollbar.push({_t:'uncaught',e:e,u:u,l:l,c:c,err:err});};var i=function(){var s=d.createElement("script");var
f=d.getElementsByTagName("script")[0];s.src="https://d37gvrvc0wt4s1.cloudfront.net/js/1/rollbar.min.js";s.async=!0;
f.parentNode.insertBefore(s,f);};if(w.addEventListener){w.addEventListener("load",i,!1);}else{w.attachEvent("onload",i);}})(window,document);
</script>