Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to send Console error logs to Loggly #6

Merged
merged 5 commits into from Dec 5, 2014
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Added support to send Console error logs to Loggly

  • Loading branch information
psquickitjayant
psquickitjayant committed Nov 26, 2014
commit a829ac63671a520159d9cac7964cd8300463b80e
@@ -4,7 +4,7 @@
LOGGLY_INPUT_SUFFIX = '.gif?',
LOGGLY_SESSION_KEY = 'logglytrackingsession',
LOGGLY_SESSION_KEY_LENGTH = LOGGLY_SESSION_KEY.length + 1;
var sendConsoleErrors;
function uuid() {
// lifted from here -> http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
@@ -15,6 +15,7 @@

function LogglyTracker() {
this.key = false;
this.sendConsoleErrors = true;
}

function setKey(tracker, key) {
@@ -23,11 +24,10 @@
setInputUrl(tracker);
}

function setConsoleError(tracker,sendConsoleErrors) {
tracker.sendConsoleErrors = sendConsoleErrors;
}
function setSendConsoleError(tracker, sendConsoleErrors) {
tracker.sendConsoleErrors = sendConsoleErrors;
}


function setInputUrl(tracker) {
tracker.inputUrl = LOGGLY_INPUT_PREFIX
+ (tracker.logglyCollectorDomain || LOGGLY_COLLECTOR_DOMAIN)
@@ -68,11 +68,10 @@
self.logglyCollectorDomain = data.logglyCollectorDomain;
return;
}

if(data.sendConsoleErrors) {
setConsoleError(self, data.sendConsoleErrors);
}
if(data.logglyKey) {
if(data.sendConsoleErrors !== undefined) {
setSendConsoleError(self, data.sendConsoleErrors);
}
if(data.logglyKey) {
setKey(self, data.logglyKey);
return;
}
@@ -81,7 +80,7 @@
return;
}
}
if(!self.key) {
if(!self.key) {
return;
}
self.track(data);
@@ -134,23 +133,22 @@
}
}

//send console error messages to Loggly
window.onerror = function (msg, url, line, col){
//send console error messages to Loggly
window.onerror = function (msg, url, line, col){

This comment has been minimized.

@vhalbwachs

vhalbwachs Dec 3, 2014
Contributor

If someone assigns an event handler to window.onerror before our script loads, this will overwrite it.

To be safe, we should probably do the following:
*Wait until a user explicitly sets setConsoleError = true before overriding window.onerror (so basically move window.onerror assignment into setSendConsoleErrors). If setConsoleError is never specified, it's defaulted to false.

*Update readme to explain new proprty (I can help with that if you want)

<script type="text/javascript" src="/js/loggly.tracker.js" async></script>
<script>
  var _LTracker = _LTracker || [];
  _LTracker.push({
    'logglyKey': '8c518f97-e3e0-4bfb-a8ed-582d084a5289',
    'setConsoleError': true
  });
</script> 

*Also, just to be safe, we could also save a reference to window.onerror before overwriting it, then calling that as well so we reduce the risk of breaking functionality on a clients page in case they don't realize some library depends on window.onerror as well. So for example, you could do something like this:

var _onerror = window.onerror;
window.onerror = function(msg, url, line, col) {
    if (tracker.sendConsoleErrors) {
        tracker.push({
            category: 'BrowserJsException',
            exception: {
                message: msg,
                url: url,
                lineno: line,
                colno: col,
            }
        });
    }
    if (_onerror && typeof _onerror === 'function') {
        _onerror.apply(window, arguments);
    }
};

if(_LTracker.sendConsoleErrors){
_LTracker.push(
{
category: 'BrowserJsException',
exception:
{
message: msg,
url: url,
lineno: line,
colno: col,
}
});
}
};
if(tracker.sendConsoleErrors === true){
tracker.push({
category: 'BrowserJsException',
exception:
{
message: msg,
url: url,
lineno: line,
colno: col,
}
});
}
};

window._LTracker = tracker; // default global tracker

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.