Skip to content
This repository
Browse code

Optimize log subscribers to check if the log level is sufficient befo…

…re performing an operations.
  • Loading branch information...
commit 37852726c93541aa1adc014cfb862659c9bcf349 1 parent 01059d7
Brian Durand bdurand authored
6 actionmailer/lib/action_mailer/log_subscriber.rb
... ... @@ -1,13 +1,15 @@
1 1 module ActionMailer
2 2 class LogSubscriber < ActiveSupport::LogSubscriber
3 3 def deliver(event)
  4 + return unless logger.info?
4 5 recipients = Array(event.payload[:to]).join(', ')
5   - info("\nSent mail to #{recipients} (%1.fms)" % event.duration)
  6 + info("\nSent mail to #{recipients} (#{event.duration.round(1)}ms)")
6 7 debug(event.payload[:mail])
7 8 end
8 9
9 10 def receive(event)
10   - info("\nReceived mail (%.1fms)" % event.duration)
  11 + return unless logger.info?
  12 + info("\nReceived mail (#{event.duration.round(1)}ms)")
11 13 debug(event.payload[:mail])
12 14 end
13 15
17 actionpack/lib/action_controller/log_subscriber.rb
@@ -4,6 +4,8 @@ class LogSubscriber < ActiveSupport::LogSubscriber
4 4 INTERNAL_PARAMS = %w(controller action format _method only_path)
5 5
6 6 def start_processing(event)
  7 + return unless logger.info?
  8 +
7 9 payload = event.payload
8 10 params = payload[:params].except(*INTERNAL_PARAMS)
9 11 format = payload[:format]
@@ -14,6 +16,8 @@ def start_processing(event)
14 16 end
15 17
16 18 def process_action(event)
  19 + return unless logger.info?
  20 +
17 21 payload = event.payload
18 22 additions = ActionController::Base.log_process_action(payload)
19 23
@@ -22,35 +26,36 @@ def process_action(event)
22 26 exception_class_name = payload[:exception].first
23 27 status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
24 28 end
25   - message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in %.0fms" % event.duration
  29 + message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms"
26 30 message << " (#{additions.join(" | ")})" unless additions.blank?
27 31
28 32 info(message)
29 33 end
30 34
31 35 def halted_callback(event)
32   - info "Filter chain halted as #{event.payload[:filter]} rendered or redirected"
  36 + info("Filter chain halted as #{event.payload[:filter]} rendered or redirected")
33 37 end
34 38
35 39 def send_file(event)
36   - info("Sent file %s (%.1fms)" % [event.payload[:path], event.duration])
  40 + info("Sent file #{event.payload[:path]} (#{event.duration.round(1)}ms)")
37 41 end
38 42
39 43 def redirect_to(event)
40   - info "Redirected to #{event.payload[:location]}"
  44 + info("Redirected to #{event.payload[:location]}")
41 45 end
42 46
43 47 def send_data(event)
44   - info("Sent data %s (%.1fms)" % [event.payload[:filename], event.duration])
  48 + info("Sent data #{event.payload[:filename]} (#{event.duration.round(1)}ms)")
45 49 end
46 50
47 51 %w(write_fragment read_fragment exist_fragment?
48 52 expire_fragment expire_page write_page).each do |method|
49 53 class_eval <<-METHOD, __FILE__, __LINE__ + 1
50 54 def #{method}(event)
  55 + return unless logger.info?
51 56 key_or_path = event.payload[:key] || event.payload[:path]
52 57 human_name = #{method.to_s.humanize.inspect}
53   - info("\#{human_name} \#{key_or_path} \#{"(%.1fms)" % event.duration}")
  58 + info("\#{human_name} \#{key_or_path} (\#{event.duration.round(1)}ms)")
54 59 end
55 60 METHOD
56 61 end
7 actionpack/lib/action_view/log_subscriber.rb
@@ -3,10 +3,13 @@ module ActionView
3 3 #
4 4 # Provides functionality so that Rails can output logs from Action View.
5 5 class LogSubscriber < ActiveSupport::LogSubscriber
  6 + VIEWS_PATTERN = /^app\/views\//.freeze
  7 +
6 8 def render_template(event)
  9 + return unless logger.info?
7 10 message = " Rendered #{from_rails_root(event.payload[:identifier])}"
8 11 message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
9   - message << (" (%.1fms)" % event.duration)
  12 + message << " (#{event.duration.round(1)}ms)"
10 13 info(message)
11 14 end
12 15 alias :render_partial :render_template
@@ -19,7 +22,7 @@ def logger
19 22 protected
20 23
21 24 def from_rails_root(string)
22   - string.sub("#{Rails.root}/", "").sub(/^app\/views\//, "")
  25 + string.sub("#{Rails.root}/", "").sub(VIEWS_PATTERN, "")
23 26 end
24 27 end
25 28 end
10 activerecord/lib/active_record/log_subscriber.rb
... ... @@ -1,11 +1,13 @@
1 1 module ActiveRecord
2 2 class LogSubscriber < ActiveSupport::LogSubscriber
  3 + IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
  4 +
3 5 def self.runtime=(value)
4   - Thread.current["active_record_sql_runtime"] = value
  6 + Thread.current[:active_record_sql_runtime] = value
5 7 end
6 8
7 9 def self.runtime
8   - Thread.current["active_record_sql_runtime"] ||= 0
  10 + Thread.current[:active_record_sql_runtime] ||= 0
9 11 end
10 12
11 13 def self.reset_runtime
@@ -24,9 +26,9 @@ def sql(event)
24 26
25 27 payload = event.payload
26 28
27   - return if 'SCHEMA' == payload[:name]
  29 + return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
28 30
29   - name = '%s (%.1fms)' % [payload[:name], event.duration]
  31 + name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
30 32 sql = payload[:sql].squeeze(' ')
31 33 binds = nil
32 34
2  activesupport/CHANGELOG.md
Source Rendered
@@ -197,4 +197,6 @@
197 197
198 198 * Remove deprecated ActiveSupport::JSON::Variable. *Erich Menge*
199 199
  200 +* Optimize log subscribers to check log level before doing any processing. *Brian Durand*
  201 +
200 202 Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activesupport/CHANGELOG.md) for previous changes.

0 comments on commit 3785272

Please sign in to comment.
Something went wrong with that request. Please try again.