Skip to content

Commit f251437

Browse files
committed
disable automatic explain if there is no logger [closes rails#4671]
1 parent d11347d commit f251437

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

activerecord/lib/active_record/explain.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def self.extended(base)
1919
# currently collected. A false value indicates collecting is turned
2020
# off. Otherwise it is an array of queries.
2121
def logging_query_plan # :nodoc:
22+
return yield unless logger
23+
2224
threshold = auto_explain_threshold_in_seconds
2325
current = Thread.current
2426
if threshold && current[:available_queries_for_explain].nil?

activerecord/test/cases/explain_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def connection
1414
base.connection
1515
end
1616

17-
def test_logging_query_plan
17+
def test_logging_query_plan_with_logger
1818
base.logger.expects(:warn).with do |message|
1919
message.starts_with?('EXPLAIN for:')
2020
end
@@ -24,6 +24,20 @@ def test_logging_query_plan
2424
end
2525
end
2626

27+
def test_logging_query_plan_without_logger
28+
original = base.logger
29+
base.logger = nil
30+
31+
base.logger.expects(:warn).never
32+
33+
with_threshold(0) do
34+
car = Car.where(:name => 'honda').first
35+
assert_equal 'honda', car.name
36+
end
37+
ensure
38+
base.logger = original
39+
end
40+
2741
def test_collect_queries_for_explain
2842
base.auto_explain_threshold_in_seconds = nil
2943
queries = Thread.current[:available_queries_for_explain] = []

railties/guides/source/active_record_querying.textile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,9 @@ A threshold of +nil+ disables automatic EXPLAINs.
14001400
The default threshold in development mode is 0.5 seconds, and +nil+ in test and
14011401
production modes.
14021402

1403+
INFO. Automatic EXPLAIN gets disabled if Active Record has no logger, regardless
1404+
of the value of the threshold.
1405+
14031406
h5. Disabling Automatic EXPLAIN
14041407

14051408
Automatic EXPLAIN can be selectively silenced with +ActiveRecord::Base.silence_auto_explain+:

0 commit comments

Comments
 (0)