jasonkarns
11/18/2012 - 6:23 PM

AOP Logging

AOP Logging

require 'logger'
require 'aquarium'

# mixed in to provide logging utilities
module MyLogger
  ...
  def logger
    $EVIL = self # I must pay penance
    MyLogger.logger
  end
end



Aquarium::Aspects::Aspect.new :before, :calls_to => :log, :on_type => Logger do |join_point, target_object, *args|
  original_message = join_point.context.parameters.last
  class_name = $EVIL.instance_eval { is_a?(Class)? name : self.class.name }
  join_point.context.parameters[-1] = "#{class_name}: #{original_message}"
end
require 'aquarium'
require 'my_logger'

class MyClass
  include MyLogger

  def some_method
    ...
  end



  include Aquarium::DSL

  after :some_method do |join_point, myclass, *args|
    myclass.logger.log "some useful logging message"
  end

end