Skip to content

Instantly share code, notes, and snippets.

@ryanstout
Created June 5, 2014 15:55
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 ryanstout/68a5a087c5d116f968bd to your computer and use it in GitHub Desktop.
Save ryanstout/68a5a087c5d116f968bd to your computer and use it in GitHub Desktop.
logger
module Logger
module ClassMethods
def __log(method_name)
orig_method_name = :"orig_#{method_name}"
alias_method orig_method_name, method_name.to_sym
define_method(method_name) do |*args, &block|
begin
puts "Before: #{args.inspect}"
5 / 0
rescue => e
puts "Error in logging: #{e}"
ensure
return_val = send(orig_method_name, *args, &block)
begin
puts "After: #{args.inspect}"
rescue => e
puts "Error in logging"
end
end
return return_val
end
end
end
def self.included(base)
base.extend(ClassMethods)
base.instance_methods.each do |method_name|
if method_name[0..0] =~ /[a-z]/ && ![:send].include?(method_name)
base.send(:__log, method_name)
end
end
end
end
class Something
def do_stuff
puts "Do stuff"
end
def be_awesome
puts "being awesome"
5 / 0
end
include Logger
end
something = Something.new
something.do_stuff
something.be_awesome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment