From 6ab02373eaf51083c112c4f4a3d46c3cff072bc1 Mon Sep 17 00:00:00 2001 From: Cyril Mougel Date: Fri, 3 Feb 2012 17:29:30 +0100 Subject: [PATCH] add an option in connection to log duration --- lib/mongo/connection.rb | 9 ++++++--- test/connection_test.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index b338a1e..a6a3291 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -36,7 +36,7 @@ module Mongo mongo_thread_local_accessor :connections - attr_reader :logger, :size, :auths, :primary, :safe, :host_to_try, + attr_reader :logger, :log_duration, :size, :auths, :primary, :safe, :host_to_try, :pool_size, :connect_timeout, :pool_timeout, :primary_pool, :socket_class, :op_timeout @@ -63,6 +63,8 @@ module Mongo # to a single, slave node. # @option opts [Logger, #debug] :logger (nil) A Logger instance for debugging driver ops. Note that # logging negatively impacts performance; therefore, it should not be used for high-performance apps. + # @option opts [Boolean, #debug] :log_duration (nil) A boolean to say if you want see information about log_duration on logger + # like logging, log_duration negatively impacts performance; therefore, it should not be used for high-performance apps. # @option opts [Integer] :pool_size (1) The maximum number of socket self.connections allowed per # connection pool. Note: this setting is relevant only for multi-threaded applications. # @option opts [Float] :pool_timeout (5.0) When all of the self.connections a pool are checked out, @@ -348,7 +350,7 @@ module Mongo self["admin"].command(oh) end - # Checks if a server is alive. This command will return immediately + # Checks if a server is alive. This command will return immediately # even if the server is in a lock. # # @return [Hash] @@ -383,7 +385,7 @@ module Mongo Thread.current[:socket_map] ||= {} Thread.current[:socket_map][self] ||= {} Thread.current[:socket_map][self][:writer] ||= checkout_writer - Thread.current[:socket_map][self][:reader] = + Thread.current[:socket_map][self][:reader] = Thread.current[:socket_map][self][:writer] end @@ -568,6 +570,7 @@ module Mongo if @logger write_logging_startup_message + @log_duration = opts[:log_duration] || false end should_connect = opts.fetch(:connect, true) diff --git a/test/connection_test.rb b/test/connection_test.rb index 3c1f4f3..f467673 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -148,6 +148,21 @@ class TestConnection < Test::Unit::TestCase assert output.string.include?('testing') end + def test_connection_log_duration_with_logger + output = StringIO.new + logger = Logger.new(output) + logger.level = Logger::DEBUG + connection = standard_connection(:logger => logger, :log_duration => true) + assert logger, connection.logger + assert connection.log_duration + end + + def test_connection_log_duration_without_logger + connection = standard_connection(:log_duration => true) + assert_nil connection.logger + assert !connection.log_duration + end + def test_drop_database db = @conn.db('ruby-mongo-will-be-deleted') coll = db.collection('temp')