I’ve been working on some jQuery widget for work/fun lately (github) and there is still something that is bothering me.
When i use a widget in my app, i want to be able to pass a debug option, that will enable or disable logging (using console.log) entirely only in this plugin.
best solution so far :
from :
(function( $, undefined ) {
})(jQuery);
to :
(function( $, console, undefined ) {
if (!o.debug) console = { log: function(){} };
})(jQuery, console);
There is one problem left
If i load two widgets like :
$myDiv.myWidget({debug: true});
$myDiv2.myWidget({debug: false});
I won’t have later debug from 1st instantiation. Any ideas ?
[original]
So far i’ve used a piece of code i’ve found on the internet :
var logger = function() {
var oldConsoleLog = null;
var pub = {};
pub.enableLogger = function enableLogger() {
if(oldConsoleLog == null) return;
window['console']['log'] = oldConsoleLog;
};
pub.disableLogger = function disableLogger() {
oldConsoleLog = console.log;
window['console']['log'] = function() {};
};
return pub;
}();
Then on _create() i can do sth like :
if (!o.debug) {
logger.disableLogger();
}
Looked quite good however, it is not plugin specific, as it will disable console.log for my entire app.
One important point is that i want to be able to keep console.log line logging feature (no wrapping of console.log since as far as i know / tried, it will replace logging line info with useless wrapped call line).
Thank you !
The easiest option I can think of is to override console inside your scope. Like this.