Merge pull request #97 from asinbow/fix_log_debug_level_detect

fix log debug level detect
This commit is contained in:
Tyler Brock 2012-06-01 11:38:37 -07:00
commit c76160c2f2
1 changed files with 19 additions and 24 deletions

View File

@ -1,14 +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
if @logger && (@logger.level == DEBUG_LEVEL) log(:debug, "Logging level is currently :debug which could negatively impact " +
log(:debug, "Logging level is currently :debug which could negatively impact " +
"client-side performance. You should set your logging level no lower than " + "client-side performance. You should set your logging level no lower than " +
":info in production.") ":info in production.")
end
end end
# Log a message with the given level. # Log a message with the given level.
@ -16,42 +12,41 @@ module Mongo
return unless @logger return unless @logger
case level case level
when :fatal then when :fatal then
@logger.fatal "MONGODB [FATAL] #{msg}" @logger.fatal { "MONGODB [FATAL] #{msg}" }
when :error then when :error then
@logger.error "MONGODB [ERROR] #{msg}" @logger.error { "MONGODB [ERROR] #{msg}" }
when :warn then when :warn then
@logger.warn "MONGODB [WARNING] #{msg}" @logger.warn { "MONGODB [WARNING] #{msg}" }
when :info then when :info then
@logger.info "MONGODB [INFO] #{msg}" @logger.info { "MONGODB [INFO] #{msg}" }
when :debug then when :debug then
@logger.debug "MONGODB [DEBUG] #{msg}" @logger.debug { "MONGODB [DEBUG] #{msg}" }
else else
@logger.debug "MONGODB [DEBUG] #{msg}" @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 = {})
start_time = Time.now start_time = Time.now
res = yield res = yield
if @logger && (@logger.level == DEBUG_LEVEL) log_operation(name, payload, start_time)
log_operation(name, payload, start_time)
end
res res
end end
protected protected
def log_operation(name, payload, start_time) def log_operation(name, payload, start_time)
msg = "MONGODB " @logger && @logger.debug do
msg << "(#{((Time.now - start_time) * 1000).to_i}ms) " msg = "MONGODB "
msg << "#{payload[:database]}['#{payload[:collection]}'].#{name}(" msg << "(#{((Time.now - start_time) * 1000).to_i}ms) "
msg << payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")" msg << "#{payload[:database]}['#{payload[:collection]}'].#{name}("
msg << ".skip(#{payload[:skip]})" if payload[:skip] msg << payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")"
msg << ".limit(#{payload[:limit]})" if payload[:limit] msg << ".skip(#{payload[:skip]})" if payload[:skip]
msg << ".sort(#{payload[:order]})" if payload[:order] msg << ".limit(#{payload[:limit]})" if payload[:limit]
msg << ".sort(#{payload[:order]})" if payload[:order]
@logger.debug(msg) msg
end
end end
end end