Methods
Since
console
isn’t yet formally covered by a specification, implementations vary. In Node it’s closely tied to standard input output, referred to as “stdio” by the documentation. This gives rise to error
and log
methods that will be printed to the appropriate output stream.
Most implementations give us convenient ways of separating output for debugging messages, errors, and warnings:
console.log('This is merely an informational debug message');
console.warn('This is a warning');
console.error('This is an error');
Inspection and Concatenation
In most browsers and Node, objects will be automatically printed in a readable format when using
console.log
. That means printing variables is possible without any extra effort:console.log({ alex: "some dude" });
How to handle Console.log Programmatic globally?
// Declare flag values used as parameters to Fixconsole() method.
var ALERT_ON = false; // Note: if 'true' -> all console logs become alert() var DEBUG_MODE_ON = true; // This enables console.log()
/** Call once at beginning to ensure your app can safely call console.log()
* and console.dir(), even on browsers that don't support it.
* You may not get useful logging on those browsers,
* but at least you won't generate errors.
* @param alertFallback If 'true', all logs become alerts, if necessary.
* (not usually suitable for production)
**/
function fixConsole(alertFallback,debugModeON) {
//alert('fixconsole -> alertFallback ='+alertFallback+'; debugModeON ='+debugModeON);
if (typeof console === "undefined") {
console = {}; // define it if it doesn't exist already
}
if (typeof console.log === "undefined") {
if (alertFallback) {
console.log = function(msg) {
alert(msg);
};
} else {
console.log = function() {};
}
}else{
//debug mode on = true
if (!debugModeON) { console = console || {}; console.log = function(){};}
}
if (typeof console.dir === "undefined") {
if (alertFallback) {
// THIS COULD BE IMPROVED - maybe list all the object properties? console.dir = function(obj) {
alert("DIR: "+obj);
};
} else {
console.dir = function() {};
}
}else{
// debug mode on = true
if (!debugModeON) { console = console || {}; console.dir = function(){}; }
}
}
Conclusion
The
console
object provides a surprisingly useful amount of functionality. If you’re writing lightweight Node programs, or want to debug something in a relatively modern browser (or browser with suitable developer tools), then try to take advantage of it rather than relying on unnecessary libraries.
In general, when logging with
console
:- Use the correct logging method so redirection works as expected
- Quickly append variables to messages using
console.log('Message:', value)
- Use
console
to automatically inspect variables
Places you can view the console! Just to have them all in one answer.
Firefox
(you can also now use Firefox's built in developer tools Ctrl+Shift+J (Tools > Web Developer > Error Console), but Firebug is much better; use Firebug)
Safari and Chrome
Basically the same.
Internet Explorer
Don't forget you can use compatibility modes to debug IE7 and IE8 in IE9 or IE10
If you must access the console in IE6 for IE7 use the Firebug Lite bookmarklet
http://getfirebug.com/firebuglite/ look for stable bookmarklet
Opera
iOS
Works for all iPhones, iPod touch and iPads.
Now with iOS 6 you can view the console through Safari in OS X if you plug in your device. Or you can do so with the emulator, simply open a Safari browser window and go to the "Develop" tab. There you will find options to get the Safari inspector to communicate with your device.
Windows Phone, Android
Both of these have no console built in and no bookmarklet ability. So we use http://jsconsole.com/ type :listen and it will give you a script tag to place in your HTML. From then on you can view your console inside the jsconsole website.
iOS and Android
You can also use http://html.adobe.com/edge/inspect/ to access web inspector tools and the console on any device using their convenient browser plugin.
Older browser problems
Lastly older browsers (thanks again Microsoft) will crash if you use console.log in your code and not have the developer tools open at the same time. Luckily its an easy fix. Simple use the below code snippet at the top of your code and good old IE should leave you alone:
if(!window.console){ window.console = {log: function(){} }; }
Comments
Post a Comment