diff options
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 8 | ||||
-rwxr-xr-x | activerecord/test/base_test.rb | 53 |
2 files changed, 57 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2a3fe1bde4..512b04bcdf 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1175,13 +1175,13 @@ module ActiveRecord #:nodoc: # project.milestones << Milestone.find(:all) # end # - # The benchmark is only recorded if the current level of the logger matches the <tt>log_level</tt>, which makes it - # easy to include benchmarking statements in production software that will remain inexpensive because the benchmark - # will only be conducted if the log level is low enough. + # The benchmark is only recorded if the current level of the logger is less than or equal to the <tt>log_level</tt>, + # which makes it easy to include benchmarking statements in production software that will remain inexpensive because + # the benchmark will only be conducted if the log level is low enough. # # The logging of the multiple statements is turned off unless <tt>use_silence</tt> is set to false. def benchmark(title, log_level = Logger::DEBUG, use_silence = true) - if logger && logger.level == log_level + if logger && logger.level <= log_level result = nil seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield } logger.add(log_level, "#{title} (#{'%.5f' % seconds})") diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 3a5fdac1ae..e3bc6ac398 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -1742,4 +1742,57 @@ class BasicsTest < Test::Unit::TestCase assert_kind_of Reply, topics(:first).becomes(Reply) assert_equal "The First Topic", topics(:first).becomes(Reply).title end + + def test_silence_sets_log_level_to_error_in_block + original_logger = ActiveRecord::Base.logger + log = StringIO.new + ActiveRecord::Base.logger = Logger.new(log) + ActiveRecord::Base.logger.level = Logger::DEBUG + ActiveRecord::Base.silence do + ActiveRecord::Base.logger.warn "warn" + ActiveRecord::Base.logger.error "error" + end + assert_equal "error\n", log.string + ensure + ActiveRecord::Base.logger = original_logger + end + + def test_silence_sets_log_level_back_to_level_before_yield + original_logger = ActiveRecord::Base.logger + log = StringIO.new + ActiveRecord::Base.logger = Logger.new(log) + ActiveRecord::Base.logger.level = Logger::WARN + ActiveRecord::Base.silence do + end + assert_equal Logger::WARN, ActiveRecord::Base.logger.level + ensure + ActiveRecord::Base.logger = original_logger + end + + def test_benchmark_with_log_level + original_logger = ActiveRecord::Base.logger + log = StringIO.new + ActiveRecord::Base.logger = Logger.new(log) + ActiveRecord::Base.logger.level = Logger::WARN + ActiveRecord::Base.benchmark("Debug Topic Count", Logger::DEBUG) { Topic.count } + ActiveRecord::Base.benchmark("Warn Topic Count", Logger::WARN) { Topic.count } + ActiveRecord::Base.benchmark("Error Topic Count", Logger::ERROR) { Topic.count } + assert_no_match /Debug Topic Count/, log.string + assert_match /Warn Topic Count/, log.string + assert_match /Error Topic Count/, log.string + ensure + ActiveRecord::Base.logger = original_logger + end + + def test_benchmark_with_use_silence + original_logger = ActiveRecord::Base.logger + log = StringIO.new + ActiveRecord::Base.logger = Logger.new(log) + ActiveRecord::Base.benchmark("Logging", Logger::DEBUG, true) { ActiveRecord::Base.logger.debug "Loud" } + ActiveRecord::Base.benchmark("Logging", Logger::DEBUG, false) { ActiveRecord::Base.logger.debug "Quiet" } + assert_no_match /Loud/, log.string + assert_match /Quiet/, log.string + ensure + ActiveRecord::Base.logger = original_logger + end end |