var Fb = {}; //An empty object literal for holding the function
Fb.log = function(obj, consoleMethod) {
if (window.console && window.console.firebug && window.console.firebug.replace(/^\s\s*/, '').replace(/\s\s*$/, '') !== '') {
if (typeof consoleMethod === "string" && typeof console[consoleMethod] === "function") {
console[consoleMethod](obj);
} else {
console.log(obj);
}
}
}
If you leave console.log, console.info, etc messages in your JavaScript code and open the page in browser like IE then it may halt the page loading completely saying that ‘console not defined’ especially if your user uses a non IE8 browser.
This code snippet will allow you to leave the logging message as such in your code if you wish and your page will render properly in IE or any other browser that does not support the console messages.
Usage
Fb.log("This will be logged");
Fb.log("This will be displayed in console as info", "info");
The FB.log function accepts two parameters the first one is the “item” that you want to display in the firebug console and the second one is the firebug method that you want to use for the logging, like info, error, etc. If you omit the second parameter the result will be equivalent to console.log()
Simple log-only way
function ltc(what) {
try {
console.log(what);
}
catch (e) {}
finally {
return;
}
}
ltc("message");
Or (at the top of your code) you could just overwrite console.log() entirely if it doesn’t exist with an empty function.
This removes the evaluation of the condition on every call, and keeps the function call uniform to other applications. New developers to the project will immediately realize what console.log() is for, unlike possible confusion that may arise from Fb.log().
Hi Benjamin,
1. The approach you’ve mentioned will work if someone uses only console.log from within the JS.
The coders may use every logging functions supported by Firebug like info, error, etc
In such a situation this may be difficult.
2. I have used a custom object literal just because this is a custom function and thought of avoiding tinkering with Firebug’s console object.
Thanks!
I missed rownumbers though, so I modified this a little and added that, together with “support” for
console.count
&console.time|timeEnd
To show an error, you’d do this, where 55 is your files row number.
And a simple “info” would be this:
And to count ( ref: http://bit.ly/d5QdJi ) write this:
To time events you only need 2 parameters, a unique number/string and the words “time” & “timeEnd”.
So to stop this timer you use:
Sorry for the multiple comments, but I thought this could come to some use…
If all you want is to avoid the error thrown in Firefox, this should work just fine:
I was wondering if there is a better way in which to handel this or is this code still good today?
window.console && console.log(“ see my code in the comment below”);
There is a way to keep it low, it uses javascript’s features to safely wrap console.loggings
–> window.console && console.log(“This single line execution won’t disappoint you !!!”);
Be careful: the window.console.firebug was removed at firebug 1.9.0 (See in https://getfirebug.com/wiki/index.php/Console_API#Note and http://code.google.com/p/fbug/issues/detail?id=4772.
I have recently created a console.log script that mimics the browser console. So no need for Firebug Lite.
https://github.com/mikewgd/console-log