carl: Archive for January, 2008

JavaScript Error Reporting with Splunk

Keeping track of new browser releases these days can be really challenging. It is less than ideal if your payment processor is throwing a JavaScript onsubmit exception effectively canceling all transactions.

Here is a little technique for indexing JavaScript exceptions in your production and development environments using Splunk.

In JavaScript create an onerror event handler that makes an HTTP request to a server that has access logs indexed by Splunk.


    function JSErrorLogger(httpBeacon){
        var self = this;
        self.handler = function(msg, url, line){
            var log = {
                "date":new Date(),
                "type":"jserror",
                "line":line,
                "msg":msg,
                "url":url
            }
            var logStr = "";
            for(var i in log){
                logStr += i + ":" + log[i] + ” “;
            }
            var imgObj = new Image();
            imgObj.src = httpBeacon + “?” + logStr;
        };
        self.JSErrorLogger = function(){
            window.onerror = self.handler;
        }();
    }

Make sure that this JavaScript is the very first item executed by the interpreter, ensuring all exceptions are caught by the event handler.

Instantiate the class with a URI that points to a beacon on a machine that has Splunk indexing the access log. You may want to set some environment variables in JavaScript that turn logging on for only testing and production machines.