public
Last active

loggable()

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
* 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;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.