Added logger convenience methods to connection and database. Makes it possible to use the logger instance in the driver from the outside world.

This commit is contained in:
John Nunemaker 2009-10-08 07:39:36 +08:00 committed by Mike Dirolf
parent 1fc537c78e
commit 89d420780f
4 changed files with 31 additions and 0 deletions

View File

@ -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.

View File

@ -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

View File

@ -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')

View File

@ -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')