Enable log duration by default for :debug-level logging.

This commit is contained in:
Kyle Banker 2012-02-16 14:00:52 -05:00
parent 8bdb64721b
commit b79d408a11
7 changed files with 38 additions and 51 deletions

View File

@ -87,7 +87,6 @@ module Mongo
@db, @name = db, name @db, @name = db, name
@connection = @db.connection @connection = @db.connection
@logger = @connection.logger @logger = @connection.logger
@log_duration = @connection.log_duration
@cache_time = @db.cache_time @cache_time = @db.cache_time
@cache = Hash.new(0) @cache = Hash.new(0)
unless pk_factory unless pk_factory

View File

@ -36,7 +36,7 @@ module Mongo
mongo_thread_local_accessor :connections mongo_thread_local_accessor :connections
attr_reader :logger, :log_duration, :size, :auths, :primary, :safe, :host_to_try, attr_reader :logger, :size, :auths, :primary, :safe, :host_to_try,
:pool_size, :connect_timeout, :pool_timeout, :pool_size, :connect_timeout, :pool_timeout,
:primary_pool, :socket_class, :op_timeout :primary_pool, :socket_class, :op_timeout
@ -63,8 +63,6 @@ module Mongo
# to a single, slave node. # to a single, slave node.
# @option opts [Logger, #debug] :logger (nil) A Logger instance for debugging driver ops. Note that # @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. # 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 # @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. # 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, # @option opts [Float] :pool_timeout (5.0) When all of the self.connections a pool are checked out,
@ -558,11 +556,10 @@ module Mongo
@primary = nil @primary = nil
@primary_pool = nil @primary_pool = nil
@logger = opts[:logger] || nil @logger = opts.fetch(:logger, nil)
if @logger if @logger
write_logging_startup_message write_logging_startup_message
@log_duration = opts[:log_duration] || false
end end
should_connect = opts.fetch(:connect, true) should_connect = opts.fetch(:connect, true)

View File

@ -43,7 +43,6 @@ module Mongo
@collection = collection @collection = collection
@connection = @db.connection @connection = @db.connection
@logger = @connection.logger @logger = @connection.logger
@log_duration = @connection.log_duration
# Query selector # Query selector
@selector = opts[:selector] || {} @selector = opts[:selector] || {}

View File

@ -1,8 +1,10 @@
module Mongo module Mongo
module Logging module Logging
DEBUG_LEVEL = defined?(Logger) ? Logger::DEBUG : 0
def write_logging_startup_message def write_logging_startup_message
log(:warn, "Please note that logging negatively impacts client-side performance. " + log(:debug, "Please note that logging negatively impacts client-side performance. " +
"You should set your logging level no lower than :info in production.") "You should set your logging level no lower than :info in production.")
end end
@ -10,42 +12,43 @@ module Mongo
def log(level, msg) def log(level, msg)
return unless @logger return unless @logger
case level case level
when :debug then
@logger.debug "MONGODB [DEBUG] #{msg}"
when :warn then
@logger.warn "MONGODB [WARNING] #{msg}"
when :error then
@logger.error "MONGODB [ERROR] #{msg}"
when :fatal then when :fatal then
@logger.fatal "MONGODB [FATAL] #{msg}" @logger.fatal "MONGODB [FATAL] #{msg}"
else when :error then
@logger.error "MONGODB [ERROR] #{msg}"
when :warn then
@logger.warn "MONGODB [WARNING] #{msg}"
when :info then
@logger.info "MONGODB [INFO] #{msg}" @logger.info "MONGODB [INFO] #{msg}"
when :debug then
@logger.debug "MONGODB [DEBUG] #{msg}"
else
@logger.debug "MONGODB [DEBUG] #{msg}"
end end
end end
# Execute the block and log the operation described by name and payload. # Execute the block and log the operation described by name and payload.
def instrument(name, payload = {}, &blk) def instrument(name, payload = {}, &blk)
time_elapse = @log_duration ? Time.now : nil start_time = Time.now
res = yield res = yield
log_operation(name, payload, time_elapse) if @logger && (@logger.level == DEBUG_LEVEL)
log_operation(name, payload, start_time)
end
res res
end end
protected protected
def log_operation(name, payload, time_elapse=nil) def log_operation(name, payload, start_time)
@logger ||= nil msg = "MONGODB "
return unless @logger msg << "(#{((Time.now - start_time) * 1000).to_i}ms) "
msg = "#{payload[:database]}['#{payload[:collection]}'].#{name}(" msg << "#{payload[:database]}['#{payload[:collection]}'].#{name}("
msg += payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")" msg << payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")"
msg += ".skip(#{payload[:skip]})" if payload[:skip] msg << ".skip(#{payload[:skip]})" if payload[:skip]
msg += ".limit(#{payload[:limit]})" if payload[:limit] msg << ".limit(#{payload[:limit]})" if payload[:limit]
msg += ".sort(#{payload[:order]})" if payload[:order] msg << ".sort(#{payload[:order]})" if payload[:order]
if time_elapse
@logger.debug "MONGODB (#{Time.now - time_elapse}s) #{msg}" @logger.debug(msg)
else
@logger.debug "MONGODB #{msg}"
end
end end
end end

View File

@ -141,8 +141,8 @@ class TestConnection < Test::Unit::TestCase
output = StringIO.new output = StringIO.new
logger = Logger.new(output) logger = Logger.new(output)
logger.level = Logger::DEBUG logger.level = Logger::DEBUG
connection = standard_connection(:logger => logger, :log_duration => true).db(MONGO_TEST_DB) connection = standard_connection(:logger => logger).db(MONGO_TEST_DB)
assert output.string.index(/\(0.\d+s\)/) assert_match /\(\d+ms\)/, output.string
assert output.string.include?("admin['$cmd'].find") assert output.string.include?("admin['$cmd'].find")
end end
@ -157,21 +157,6 @@ class TestConnection < Test::Unit::TestCase
assert output.string.include?('testing') assert output.string.include?('testing')
end 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 def test_drop_database
db = @conn.db('ruby-mongo-will-be-deleted') db = @conn.db('ruby-mongo-will-be-deleted')
coll = db.collection('temp') coll = db.collection('temp')

View File

@ -5,7 +5,8 @@ class CollectionTest < Test::Unit::TestCase
context "Basic operations: " do context "Basic operations: " do
setup do setup do
@logger = mock() @logger = mock()
@logger.expects(:warn) @logger.stubs(:level => 0)
@logger.expects(:debug)
end end
should "send update message" do should "send update message" do

View File

@ -5,8 +5,10 @@ class CursorTest < Test::Unit::TestCase
setup do setup do
@logger = mock() @logger = mock()
@logger.stubs(:debug) @logger.stubs(:debug)
@connection = stub(:class => Connection, :logger => @logger, :slave_ok? => false, :read_preference => :primary) @connection = stub(:class => Connection, :logger => @logger,
@db = stub(:name => "testing", :slave_ok? => false, :connection => @connection, :read_preference => :primary) :slave_ok? => false, :read_preference => :primary, :log_duration => false)
@db = stub(:name => "testing", :slave_ok? => false,
:connection => @connection, :read_preference => :primary)
@collection = stub(:db => @db, :name => "items", :read_preference => :primary) @collection = stub(:db => @db, :name => "items", :read_preference => :primary)
@cursor = Cursor.new(@collection) @cursor = Cursor.new(@collection)
end end
@ -100,7 +102,8 @@ class CursorTest < Test::Unit::TestCase
setup do setup do
@logger = mock() @logger = mock()
@logger.stubs(:debug) @logger.stubs(:debug)
@connection = stub(:class => Connection, :logger => @logger, :slave_ok? => false) @connection = stub(:class => Connection, :logger => @logger, :slave_ok? => false,
:log_duration => false)
@db = stub(:slave_ok? => true, :name => "testing", :connection => @connection) @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection)
@collection = stub(:db => @db, :name => "items", :read_preference => :primary) @collection = stub(:db => @db, :name => "items", :read_preference => :primary)
end end