Eclipse IDE console.log objects in Javascript

cordova, eclipse, javascript

Solution

You can write your own console "tree" logger and include it in your source. A simple example can be the following:

function logObject(obj)
{
    var ind = "";
    if (arguments.length > 1)
    {
        ind = arguments[1];
    }

    if (typeof obj == "undefined" || obj == null)
    {
        console.log("<null>");
        return;
    }

    if (typeof obj != "object")
    {
        console.log(obj);
        return;
    }

    for (var key in obj)
    {
        if (typeof obj[key] == "object")
        {
            console.log(ind + key + "={");
            logObject(obj[key], ind + "  ");
            console.log(ind + "}");
        }
        else
        {
            console.log(ind + key + "=" + obj[key]);
        }
    }
}

Usage example:

var a = [1, 2, 3];
var o = {a:1, b:2, c:3, d: a};
var obj = {a:1, b:o, c:3, d: a};

logObject(obj);

The result will look like this in the console or in the LogCat:

a=1
b={
  a=1
  b=2
  c=3
  d={
    0=1
    1=2
    2=3
  }
}
c=3
d={
  0=1
  1=2
  2=3
}

Cheers

Problem

New user to Eclipse IDE, I figured `console.log()` would print the string to the logcat panel which is great. But when i try to `console.log()` an object (instead of string literal), all i see in console is [object object]. I'm using `console.log(JSON.stringify(MyObject))` to workaround this at the moment, it works for a simple object, however, if the object is too complex, the string got truncated at some point, which makes my debugging a lot harder. Is there a neat way to inspect an object in the console panel with a nice treeview like what you'd get in Chrome developer tool (An expandable treeview is always easier to deal with json string)? It is also worth mentioning that I'm doing PhoneGap development for Android. So basically I'm dealing with only HTML and Javascript. System.out.println won't work for me. *Also, I'm aware of Weinre for PhoneGap too. But I'm only looking for solution within local IDE, as the web debugging is too lag for my liking. Thanks

Original source