Module: LogMethod::Log

Defined in:
lib/log_method/log.rb

Instance Method Summary collapse

Instance Method Details

#log(method, message_or_object, message = nil) ⇒ Object



6
7
8
9
10
11
12
13
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
# File 'lib/log_method/log.rb', line 6

def log(method, message_or_object, message=nil)

  trace_id         = LogMethod.config.trace_id_proc.()
  current_actor_id = LogMethod.config.current_actor_proc.()

  object_identifier_phrase = ""

  if message.nil?
    message = message_or_object
  else
    object_id,object_class = if !logging_external_identifier_method.nil? &&
                                 message_or_object.respond_to?(logging_external_identifier_method)

                               [message_or_object.send(logging_external_identifier_method), message_or_object.class]

                             elsif message_or_object.kind_of?(ActiveRecord::Base)

                               [message_or_object.id, message_or_object.class]

                             else

                               [message_or_object.inspect, message_or_object.class]

                             end
    object_identifier_phrase = "[#{object_class}/#{object_id}]: "
  end

  Rails.logger.info("[#{self.class}##{method}](via LogMethod::Log)#{format_trace_id(trace_id)}#{format_current_actor_id(current_actor_id)}: #{object_identifier_phrase}#{message}")

  all_args = [self.class.name, method, object_id, object_class&.name, trace_id, current_actor_id, message]

  LogMethod.config.after_log_procs.each do |after_log_proc|
    arity = if after_log_proc.kind_of?(Proc)
              after_log_proc.arity
            elsif after_log_proc.respond_to?(:call)
              after_log_proc.method(:call).arity
            end
    args_for_arity = if arity <= 0
                       []
                     else
                       all_args[0..(arity-1)]
                     end
    after_log_proc.(*args_for_arity)
  end
end

#logging_external_identifier_methodObject



2
3
4
# File 'lib/log_method/log.rb', line 2

def logging_external_identifier_method
  @logging_external_identifier_method ||=  LogMethod.config.external_identifier_method
end