From 89d420780fc2728d721754a50f61b6287ac1ed85 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Thu, 8 Oct 2009 07:39:36 +0800 Subject: [PATCH] Added logger convenience methods to connection and database. Makes it possible to use the logger instance in the driver from the outside world. --- lib/mongo/connection.rb | 4 ++++ lib/mongo/db.rb | 3 +++ test/test_connection.rb | 11 +++++++++++ test/test_db.rb | 13 +++++++++++++ 4 files changed, 31 insertions(+) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 54ed48f..82a4dac 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -94,6 +94,10 @@ module Mongo def db(db_name, options={}) DB.new(db_name, @pair, @options.merge(options)) end + + def logger + @options[:logger] + end # Returns a hash containing database names as keys and disk space for # each as values. diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 7421f96..22b49c5 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -58,6 +58,9 @@ module Mongo # The database's socket. For internal (and Cursor) use only. attr_reader :socket + + # The logger instance if :logger is passed to initialize + attr_reader :logger def slave_ok?; @slave_ok; end def auto_reconnect?; @auto_reconnect; end diff --git a/test/test_connection.rb b/test/test_connection.rb index d7928d6..aab17d7 100644 --- a/test/test_connection.rb +++ b/test/test_connection.rb @@ -66,6 +66,17 @@ class TestConnection < Test::Unit::TestCase assert output.string.include?("db.test.find") assert !output.string.include?("db.test.remove") end + + def test_connection_logger + output = StringIO.new + logger = Logger.new(output) + logger.level = Logger::DEBUG + connection = Connection.new(@host, @port, :logger => logger) + assert_equal logger, connection.logger + + connection.logger.debug 'testing' + assert output.string.include?('testing') + end def test_drop_database db = @mongo.db('ruby-mongo-will-be-deleted') diff --git a/test/test_db.rb b/test/test_db.rb index 3ae2033..9f1dfd2 100644 --- a/test/test_db.rb +++ b/test/test_db.rb @@ -2,6 +2,8 @@ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') require 'digest/md5' require 'mongo' require 'test/unit' +require 'stringio' +require 'logger' class TestPKFactory def create_pk(row) @@ -45,6 +47,17 @@ class DBTest < Test::Unit::TestCase @@users = @@db.collection('system.users') end end + + def test_logger + output = StringIO.new + logger = Logger.new(output) + logger.level = Logger::DEBUG + db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test') + assert_equal logger, db.logger + + db.logger.debug 'testing' + assert output.string.include?('testing') + end def test_full_coll_name coll = @@db.collection('test')