If you really want to always include using console
, I suggest using a polyfill for it:
(function(b){
var a=0;
var c=function(){};
var d=["log","assert","clear","count","debug","dir","dirxml","error","exception","group","groupCollapsed","groupEnd","info","profile","profileEnd","table","time","timeEnd","timeStamp","trace","warn"];
b.console=b.console||{};
for(;a<d.length;a++){
b.console[d[a]]=b.console[d[a]]||b.console["log"]||c;
}
})(window);
This a minified example that I tried to make readable. It's something I found awhile ago and modified some. I think the main thing I modified is that if you call a method that wasn't originally implemented by the browser's native console
, it will call console.log
. If console.log
wasn't natively implemented, it just calls an empty function. The original version of this code didn't include this fallback to console.log
.
This will "guarantee" that console
calls will not fail. You can change the d
variable to only include calls you are sure you will use, otherwise there's some extra unnecessary processing.