Created
June 5, 2014 15:55
-
-
Save ryanstout/68a5a087c5d116f968bd to your computer and use it in GitHub Desktop.
logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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