Module: InspectorGadget::Brain

Defined in:
lib/inspector_gadget.rb

Class Method Summary collapse

Class Method Details

.log_all_instance_method_calls(obj) ⇒ Object

obj either a class (instance methods) or a class singleton (class methods)



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
# File 'lib/inspector_gadget.rb', line 14

def self.log_all_instance_method_calls(obj)
  # setting up alias_method_chain
  obj.class_eval do
    (instance_methods - Object.methods).each do |method_name|
      method_name = method_name.to_s # convert symbol to string
      aliased_target, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1
      method_with_logging_name, method_without_logging_name = "#{aliased_target}_with_logging#{punctuation}", "#{aliased_target}_without_logging#{punctuation}"

      define_method(method_with_logging_name) do |*args, &block|
        args_to_log = args.dup
        filter_args_in_log(method_name, args_to_log)
        if self.is_a?(Class)
          method_type = 'class'
          class_name = self.name
        else
          method_type = 'instance'
          class_name = self.class.name
        end
        Rails.logger.info("#{class_name} LOG: #{method_type} method name: #{method_name}, args: #{args_to_log.inspect}")
        result = send(method_without_logging_name, *args, &block)
        result_to_log = result.dup rescue result
        filter_result_in_log(method_name, result_to_log)
        Rails.logger.info("#{class_name} LOG: #{method_type} method name: #{method_name}, args: #{args_to_log.inspect} returned #{result_to_log.inspect}")
        result
      end

      alias_method_chain method_name, :logging

      # override this method to hide passwords etc
      def filter_args_in_log(method_name, args)
      end

      # override this method to hide passwords etc
      def filter_result_in_log(method_name, result)
      end
    end
  end
end