Skip to content

@ryanstout /logger.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.