Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8455

Show
Ignore:
Timestamp:
12/21/07 01:48:20 (7 months ago)
Author:
bitsweat
Message:

Benchmark logs for any level below or equal to the one specified, rather than just equal. Closes #10580 [Dan Manges]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/base.rb

    r8433 r8455  
    11761176      #   end 
    11771177      # 
    1178       # The benchmark is only recorded if the current level of the logger matches the <tt>log_level</tt>, which makes it 
    1179       # easy to include benchmarking statements in production software that will remain inexpensive because the benchmark 
    1180       # will only be conducted if the log level is low enough. 
     1178      # The benchmark is only recorded if the current level of the logger is less than or equal to the <tt>log_level</tt>, 
     1179      # which makes it easy to include benchmarking statements in production software that will remain inexpensive because 
     1180      # the benchmark will only be conducted if the log level is low enough. 
    11811181      # 
    11821182      # The logging of the multiple statements is turned off unless <tt>use_silence</tt> is set to false. 
    11831183      def benchmark(title, log_level = Logger::DEBUG, use_silence = true) 
    1184         if logger && logger.level == log_level 
     1184        if logger && logger.level <= log_level 
    11851185          result = nil 
    11861186          seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield } 
  • trunk/activerecord/test/base_test.rb

    r8319 r8455  
    17431743    assert_equal "The First Topic", topics(:first).becomes(Reply).title 
    17441744  end 
     1745 
     1746  def test_silence_sets_log_level_to_error_in_block 
     1747    original_logger = ActiveRecord::Base.logger 
     1748    log = StringIO.new 
     1749    ActiveRecord::Base.logger = Logger.new(log) 
     1750    ActiveRecord::Base.logger.level = Logger::DEBUG 
     1751    ActiveRecord::Base.silence do 
     1752      ActiveRecord::Base.logger.warn "warn" 
     1753      ActiveRecord::Base.logger.error "error" 
     1754    end 
     1755    assert_equal "error\n", log.string 
     1756  ensure 
     1757    ActiveRecord::Base.logger = original_logger 
     1758  end 
     1759 
     1760  def test_silence_sets_log_level_back_to_level_before_yield 
     1761    original_logger = ActiveRecord::Base.logger 
     1762    log = StringIO.new 
     1763    ActiveRecord::Base.logger = Logger.new(log) 
     1764    ActiveRecord::Base.logger.level = Logger::WARN 
     1765    ActiveRecord::Base.silence do 
     1766    end 
     1767    assert_equal Logger::WARN, ActiveRecord::Base.logger.level 
     1768  ensure 
     1769    ActiveRecord::Base.logger = original_logger 
     1770  end 
     1771 
     1772  def test_benchmark_with_log_level 
     1773    original_logger = ActiveRecord::Base.logger 
     1774    log = StringIO.new 
     1775    ActiveRecord::Base.logger = Logger.new(log) 
     1776    ActiveRecord::Base.logger.level = Logger::WARN 
     1777    ActiveRecord::Base.benchmark("Debug Topic Count", Logger::DEBUG) { Topic.count } 
     1778    ActiveRecord::Base.benchmark("Warn Topic Count",  Logger::WARN)  { Topic.count } 
     1779    ActiveRecord::Base.benchmark("Error Topic Count", Logger::ERROR) { Topic.count } 
     1780    assert_no_match /Debug Topic Count/, log.string 
     1781    assert_match /Warn Topic Count/, log.string 
     1782    assert_match /Error Topic Count/, log.string 
     1783  ensure 
     1784    ActiveRecord::Base.logger = original_logger 
     1785  end 
     1786 
     1787  def test_benchmark_with_use_silence 
     1788    original_logger = ActiveRecord::Base.logger 
     1789    log = StringIO.new 
     1790    ActiveRecord::Base.logger = Logger.new(log) 
     1791    ActiveRecord::Base.benchmark("Logging", Logger::DEBUG, true) { ActiveRecord::Base.logger.debug "Loud" } 
     1792    ActiveRecord::Base.benchmark("Logging", Logger::DEBUG, false)  { ActiveRecord::Base.logger.debug "Quiet" } 
     1793    assert_no_match /Loud/, log.string 
     1794    assert_match /Quiet/, log.string 
     1795  ensure 
     1796    ActiveRecord::Base.logger = original_logger 
     1797  end 
    17451798end