Class: ActionController::LogSubscriber

Inherits:
ActiveSupport::EventReporter::LogSubscriber show all
Defined in:
actionpack/lib/action_controller/log_subscriber.rb

Overview

:nodoc:

Constant Summary

Constants inherited from ActiveSupport::EventReporter::LogSubscriber

ActiveSupport::EventReporter::LogSubscriber::LEVEL_CHECKS

Constants included from ActiveSupport::ColorizeLogging

ActiveSupport::ColorizeLogging::BLACK, ActiveSupport::ColorizeLogging::BLUE, ActiveSupport::ColorizeLogging::CYAN, ActiveSupport::ColorizeLogging::GREEN, ActiveSupport::ColorizeLogging::MAGENTA, ActiveSupport::ColorizeLogging::MODES, ActiveSupport::ColorizeLogging::RED, ActiveSupport::ColorizeLogging::WHITE, ActiveSupport::ColorizeLogging::YELLOW

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveSupport::EventReporter::LogSubscriber

#emit, event_log_level, #logger, subscription_filter

Methods included from ActiveSupport::ColorizeLogging

#color, #colorize_logging, #debug, #error, #fatal, #info, #mode_from, #unknown, #warn

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Class Method Details

.default_loggerObject



119
120
121
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 119

def self.default_logger
  ActionController::Base.logger
end

Instance Method Details

#callback_halted(event) ⇒ Object



42
43
44
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 42

def callback_halted(event)
  info { "Filter chain halted as #{event[:payload][:filter].inspect} rendered or redirected" }
end

#csrf_javascript_blocked(event) ⇒ Object



102
103
104
105
106
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 102

def csrf_javascript_blocked(event)
  return unless ActionController::Base.log_warning_on_csrf_failure

  warn { event[:payload][:message] }
end

#csrf_request_blocked(event) ⇒ Object



95
96
97
98
99
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 95

def csrf_request_blocked(event)
  return unless ActionController::Base.log_warning_on_csrf_failure

  warn { event[:payload][:message] }
end

#csrf_token_fallback(event) ⇒ Object



85
86
87
88
89
90
91
92
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 85

def csrf_token_fallback(event)
  return unless ActionController::Base.log_warning_on_csrf_failure

  warn do
    payload = event[:payload]
    "Falling back to CSRF token verification for #{payload[:controller]}##{payload[:action]}"
  end
end

#data_sent(event) ⇒ Object



70
71
72
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 70

def data_sent(event)
  info { "Sent data #{event[:payload][:filename]} (#{event[:payload][:duration_ms].round(1)}ms)" }
end

#file_sent(event) ⇒ Object



56
57
58
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 56

def file_sent(event)
  info { "Sent file #{event[:payload][:path]} (#{event[:payload][:duration_ms].round(1)}ms)" }
end

#fragment_cache(event) ⇒ Object



109
110
111
112
113
114
115
116
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 109

def fragment_cache(event)
  return unless ActionController::Base.enable_fragment_cache_logging

  key        = event[:payload][:key]
  human_name = event[:payload][:method].to_s.humanize

  info("#{human_name} #{key} (#{event[:payload][:duration_ms]}ms)")
end

#redirected(event) ⇒ Object



61
62
63
64
65
66
67
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 61

def redirected(event)
  info { "Redirected to #{event[:payload][:location]}" }

  if ActionDispatch.verbose_redirect_logs && (source = redirect_source_location)
    info { "↳ #{source}" }
  end
end

#request_completed(event) ⇒ Object



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

def request_completed(event)
  info do
    payload = event[:payload]
    additions = ActionController::Base.log_process_action(payload)
    status = payload[:status]

    if status.nil? && (exception_class_name = payload[:exception]&.first)
      status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
    end

    additions << "GC: #{payload[:gc_time_ms].round(1)}ms"

    message = +"Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{payload[:duration_ms].round(0)}ms" \
               " (#{additions.join(" | ")})"
    message << "\n\n" if defined?(Rails.env) && Rails.env.development?

    message
  end
end

#request_started(event) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 9

def request_started(event)
  payload = event[:payload]
  params  = payload[:params]
  format  = payload[:format]
  format  = format.to_s.upcase if format.is_a?(Symbol)
  format  = "*/*" if format.nil?

  info "Processing by #{payload[:controller]}##{payload[:action]} as #{format}"
  info "  Parameters: #{params.inspect}" unless params.empty?
end

#rescue_from_handled(event) ⇒ Object

Manually subscribed below



48
49
50
51
52
53
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 48

def rescue_from_handled(event)
  exception_class = event[:payload][:exception_class]
  exception_message = event[:payload][:exception_message]
  exception_backtrace = event[:payload][:exception_backtrace]
  info { "rescue_from handled #{exception_class} (#{exception_message}) - #{exception_backtrace}" }
end

#unpermitted_parameters(event) ⇒ Object



75
76
77
78
79
80
81
82
# File 'actionpack/lib/action_controller/log_subscriber.rb', line 75

def unpermitted_parameters(event)
  debug do
    unpermitted_keys = event[:payload][:unpermitted_keys]
    display_unpermitted_keys = unpermitted_keys.map { |e| ":#{e}" }.join(", ")
    context = event[:payload][:context].map { |k, v| "#{k}: #{v}" }.join(", ")
    color("Unpermitted parameter#{'s' if unpermitted_keys.size > 1}: #{display_unpermitted_keys}. Context: { #{context} }", RED)
  end
end