Skip to content

Instantly share code, notes, and snippets.

@johntimothybailey
Created February 27, 2012 18:32
Show Gist options
  • Select an option

  • Save johntimothybailey/1926039 to your computer and use it in GitHub Desktop.

Select an option

Save johntimothybailey/1926039 to your computer and use it in GitHub Desktop.
Log Method Invocation - An Underscore Mixin
_.mixin({
/**
* Logs method invocation of a provided instance
*
* @param target Currently must be an instance
* @param options Possible options are as follows
* prefix - A prefix String for the logging (e.g. "Person instance")
* logMethod - The function to use for doing the logging (default: console.log)
*/
logMethodInvocation : function(target,options) {
_.each(_.methods(target),function(method){
options = options || {};
if(_.isString(options)){
options = {
prefix: options
};
}
var _method = target[method];
target[method] = function(){
options.logMethod = options.logMethod || console.log;
if(options.logMethod){
options.prefix = options.prefix || "";
options.logMethod(options.prefix,method,arguments);
}
return _method.apply(target,arguments);
};
});
}
});
_.logMethodInvocation(_,"underscore method:");
// OR
_.logMethodInvocation(_,{prefix:"underscore method:",logMethod:console.log});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment