views:

818

answers:

1

I'm working on a jQuery plugin and am trying to make a uniform, cross-browser console object for debugging. The plugin will utilize Firebug or FirebugLite but also allows the user to choose the browser's native console object, if available. For Safari/Chrome, the console methods accept only 1 argument, while the Firebug console API (which is what I'm trying to replicate) accepts any number of arguments that are printed as a concatenated string. I'm using the following code to override the native console log method:

window.console._log = window.console.log;
window.console.log = function(){
    window.console._log($.makeArray(arguments).join(", "));
};

I have a test page that will fire console.log("str1", "str2") commands on click. The first few times, the log messages are working as expected, but then after some period of time (usually just a few seconds), the log messages begin to only print the first argument. It's as if the console object is being restored to its original, native state and my overridden log() method is being destroyed. Does anyone have any more information on this or might there be a workaround?

+1  A: 

Perhaps you might have more luck replacing the entire Console object? That worked for me:

window._console = window.console;
window.console = {
    log: function() {
        window._console.log($.makeArray(arguments).join(", "));
    }
};


FWIW, in the WebCore IDL source, the console property of DOMWindow is marked as Replaceable, whereas the properties of the console object are not.

Miles