Skip to content

Instantly share code, notes, and snippets.

@nikushi
Created July 12, 2016 02:14
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 nikushi/950cb68984cd86fec46e2f2ea04f96e2 to your computer and use it in GitHub Desktop.
Save nikushi/950cb68984cd86fec46e2f2ea04f96e2 to your computer and use it in GitHub Desktop.
logging when methods begin and when methods end by prepending
require 'logger'
module Logging
def self.prepended(base)
target_methods = base.instance_methods - Object.methods
target_methods.each do |method|
define_method(method) do |*args|
log.debug "[Start] #{base}##{method}"
ret = super(*args)
log.debug "[End] #{base}##{method}"
return ret
end
end
end
private
def log
@log ||= Logger.new $stdout
end
end
class A
def hoge
puts 'HOGE'
end
def fuga
puts "FUGA"
end
# this line has to be after all method definitions
prepend Logging
end
A.new.hoge
A.new.fuga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment