Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

logger

View logger.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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
Something went wrong with that request. Please try again.