Monday, May 7, 2012

Javascript Logging

Quick Reference
console.log(object[, object, ...])
console.debug(object[, object, ...])
console.info(object[, object, ...])
console.warn(object[, object, ...])
console.error(object[, object, ...])
console.assert(expression[, object, ...])
console.dir(object)
console.dirxml(node)
console.trace()
console.group(object[, object, ...])
console.groupEnd()
console.time(name)
console.timeEnd(name)
console.profile([title])
console.profileEnd()
console.count([title])

Examples
console.log("My name is %s, and I am %d years old", 'Jim', 30);
String Substitution Patterns
%s String
%d, %i Integer (numeric formatting is not yet supported)
%f Floating point number (numeric formatting is not yet supported)
%o Object hyperlink


Cross Browsers
Firefox (Firebug/FireBug Lite)
Chrome
Safari
Opera
IE

Option 1:
window.console||(window.console={log:function(){}});

Option 2:
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    if (arguments.length == 1) {
        console.log(arguments[0]);
    }
    else {
        console.log( Array.prototype.slice.call(arguments) );
    }
  }
};

Reference
http://geekswithblogs.net/renso/archive/2009/07/02/firebug-console-quick-reference-guide.aspx
http://getfirebug.com/logging
http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

No comments:

Post a Comment