I'm now developing an application, and place a global isDebug
switch. I would like to wrap console.log
for more convenient usage.
//isDebug controls the entire site.
var isDebug = true;
//debug.js
function debug(msg, level){
var Global = this;
if(!(Global.isDebug && Global.console && Global.console.log)){
return;
}
level = level||'info';
Global.console.log(level + ': '+ msg);
}
//main.js
debug('Here is a msg.');
Then I get this result in Firefox console.
info: Here is a msg. debug.js (line 8)
What if I want to log with line number where debug()
gets called, like info: Here is a msg. main.js (line 2)
?
console.log
for info,console.warn
for warning andconsole.error
for error, instead of adding something inconsole.log
via a wrapper function. – Alvin Wong Dec 11 '12 at 7:40console
needs to be used. To achieve such goal, a wrapper seems to be the only way? – Rufus Dec 11 '12 at 7:44