Skip to content

Instantly share code, notes, and snippets.

@subchild
Created August 24, 2010 17:28
Show Gist options
  • Save subchild/547926 to your computer and use it in GitHub Desktop.
Save subchild/547926 to your computer and use it in GitHub Desktop.
loggable()
/**
* Loggable adds a log method to the passed object.
* @param obj {Object} Object which will get a new log() method
* @param objName {String} Optional parameter for displaying a string before each log output
* @param debugMode {boolean} Optional switch for disabling logging
*/
var loggable = function(obj /* , objName, debugMode */){
var objName = arguments[1] || "",
debugMode = (typeof arguments[2]!=="undefined") ? arguments[2] : true;
prefix = objName ? objName + ": " : "";
obj.log = (function(prefix){
return function(){
if (debugMode && typeof console!=="undefined"){
if (arguments.length){
arguments[0] = prefix + arguments[0];
}
console.log.apply(null, arguments);
}
}
})(prefix);
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment