Logging in Javascript

Today’s Ajaxian article about the Blackbird Javascript logging library has prompted a stream of comments asking what advantage this gives over console.log() in Firebug.

I think both the commenters and the Blackbird author have missed a very important aspect of logging in Javascript, namely the ability to record errors outside your own session, i.e. server-side.

This is all that’s missing (in roughly JQuery syntax for brevity):

window.onerror = function(e)
{
    // get the error data
    if (!e) e = window.error;
    // save error and UI activity for context
    $.post( log_url, 
        { 
            error:e.toString(), 
            lines:logger.getRecentLines(20) 
        }, 
        function(){
            // do callback if necessary
        });
};
Advertisements

One Response to Logging in Javascript

  1. Tim Down says:

    log4javascript (http://log4javascript.org) has the ability to log to the server (using its AjaxAppender).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: