From 291c667d6614ce28f85311f06ceb5f130b903be1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 6 Sep 2005 17:07:08 +0000 Subject: Only do benchmarking if log level matches and log caching git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2141 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/benchmarking.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_controller/benchmarking.rb') diff --git a/actionpack/lib/action_controller/benchmarking.rb b/actionpack/lib/action_controller/benchmarking.rb index b7c7eae8e9..587bc29d42 100644 --- a/actionpack/lib/action_controller/benchmarking.rb +++ b/actionpack/lib/action_controller/benchmarking.rb @@ -19,11 +19,15 @@ module ActionController #:nodoc: module ClassMethods # Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it # (unless use_silence is set to false). - def benchmark(title, use_silence = true) - if logger + # + # The benchmark is only recorded if the current level of the logger matches the log_level, 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. + def benchmark(title, log_level = Logger::DEBUG, use_silence = true) + if logger && logger.level == log_level result = nil seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield } - logger.info "#{title} (#{sprintf("%f", seconds)})" + logger.add(log_level, "#{title} (#{sprintf("%f", seconds)})") result else yield -- cgit v1.2.3