Class: Sails::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/sails/log_subscriber.rb

Constant Summary collapse

INTERNAL_PARAMS =
%w(controller action format _method only_path)

Instance Method Summary collapse

Instance Method Details

#loggerObject


41
42
43
# File 'lib/sails/log_subscriber.rb', line 41

def logger
  Sails.logger
end

#process_action(event) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sails/log_subscriber.rb', line 18

def process_action(event)
  info do
    payload   = event.payload
    additions = []

    payload.each_key do |key|
      key_s = key.to_s
      if key_s.include?("_runtime")
        next if payload[key].blank?
        runtime_name = key_s.sub("_runtime","").capitalize
        additions << ("#{runtime_name}: %.1fms" % payload[key].to_f)
      end
    end

    status = payload[:status]
    payload[:runtime] = event.duration

    message = "Completed #{status} in #{event.duration.round(2)}ms"
    message << " (#{additions.join(" | ")})"
    message
  end
end

#start_processing(event) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sails/log_subscriber.rb', line 5

def start_processing(event)
  return unless logger.info?

  payload = event.payload
  params = payload[:params]

  info ""
  info "Processing by #{payload[:controller]}##{payload[:action]} at #{Time.now}"
  info "  Parameters: #{params.except(:method_name).inspect}" unless params.empty?

  ActiveRecord::LogSubscriber.reset_runtime if defined?(ActiveRecord::LogSubscriber)
end