Module: TransactionLogger::ClassMethods

Defined in:
lib/transaction_logger.rb

Instance Method Summary collapse

Instance Method Details

#add_transaction_log(method, options = {}) ⇒ Object

Registers a method with the TransactionLogger, which will begin tracking the logging within the method and it's nested methods. These logs are collected under one transaction, and if the level threshold is broken or when an error is raised, the collected logs are pushed to the configured logger as a JSON hash.

By registering multiple methods as transactions, each method becomes it's own transaction.

Parameters:

  • The method you want to register with TransactionLogger

  • (defaults to: {})

    Additional options, such as custom transaction name and context



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
57
58
59
60
61
62
63
64
# File 'lib/transaction_logger.rb', line 28

def add_transaction_log(method, options={})
  old_method = instance_method method

  options = {}
  options[:prefix] = TransactionLogger::Configure.instance_variable_get :@prefix
  options[:logger] = TransactionLogger::Configure.instance_variable_get :@logger
  options[:level_threshold] = TransactionLogger::Configure.instance_variable_get :@level_threshold

  define_method method do
    TransactionManager.start options, lambda  { |transaction|
      transaction.name = options[:name]
      transaction.name ||= "#{old_method.bind(self).owner}#{method.inspect}"
      transaction.context = options[:context]
      transaction.context ||= {}

      # Check for a logger on the instance
      if methods.include? :logger
        logger_method = method(:logger).unbind
      # Check for a logger on the class
      elsif self.class.methods.include? :logger
        logger_method = self.class.method :logger
      end

      # Trap the logger if we've found one
      if logger_method
        method_info = {}
        method_info[:logger_method] = logger_method
        method_info[:calling_method] = caller_locations(1, 1)[0].label
        method_info[:includer] = self

        TransactionLogger::Helper.trap_logger method, transaction, method_info
      end

      old_method.bind(self).call
    }
  end
end