Skip to content

Instantly share code, notes, and snippets.

@styx
Forked from nhance/method_logger.rb
Created September 25, 2012 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save styx/3780374 to your computer and use it in GitHub Desktop.
Save styx/3780374 to your computer and use it in GitHub Desktop.
Rails compatible method logging. Use this to log all calls to instance methods of a class to the log.
Model.new.foo
module MethodLogger
def self.included(base)
methods = base.instance_methods(false) + base.private_instance_methods(false)
base.class_eval do
methods.each do |method_name|
original_method = instance_method(method_name)
define_method(method_name) do |*args, &block|
Rails.logger.info "-> #{base}##{method_name}(#{args.inspect})"
return_value = original_method.bind(self).call(*args, &block)
Rails.logger.info "<- #{base}##{method_name} #=> #{return_value.inspect}"
return_value
end
end
end
end
end
class Model
include MethodLogging
def foo
"bar"
end
end
-> foo([])
<- foo #=> "bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment