2011-09-11 16:08:26 +00:00
|
|
|
module Mongo
|
|
|
|
module Logging
|
|
|
|
|
2012-02-16 19:00:52 +00:00
|
|
|
DEBUG_LEVEL = defined?(Logger) ? Logger::DEBUG : 0
|
|
|
|
|
2011-11-15 20:59:42 +00:00
|
|
|
def write_logging_startup_message
|
2012-03-07 18:23:00 +00:00
|
|
|
if @logger && (@logger.level == DEBUG_LEVEL)
|
|
|
|
log(:debug, "Logging level is currently :debug which could negatively impact " +
|
|
|
|
"client-side performance. You should set your logging level no lower than " +
|
|
|
|
":info in production.")
|
|
|
|
end
|
2011-11-15 20:59:42 +00:00
|
|
|
end
|
|
|
|
|
2011-09-11 16:08:26 +00:00
|
|
|
# Log a message with the given level.
|
2011-09-13 21:50:01 +00:00
|
|
|
def log(level, msg)
|
2011-09-11 16:08:26 +00:00
|
|
|
return unless @logger
|
|
|
|
case level
|
|
|
|
when :fatal then
|
|
|
|
@logger.fatal "MONGODB [FATAL] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
when :error then
|
|
|
|
@logger.error "MONGODB [ERROR] #{msg}"
|
|
|
|
when :warn then
|
|
|
|
@logger.warn "MONGODB [WARNING] #{msg}"
|
|
|
|
when :info then
|
2011-09-11 16:08:26 +00:00
|
|
|
@logger.info "MONGODB [INFO] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
when :debug then
|
|
|
|
@logger.debug "MONGODB [DEBUG] #{msg}"
|
|
|
|
else
|
|
|
|
@logger.debug "MONGODB [DEBUG] #{msg}"
|
2011-09-11 16:08:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Execute the block and log the operation described by name and payload.
|
|
|
|
def instrument(name, payload = {}, &blk)
|
2012-02-16 19:00:52 +00:00
|
|
|
start_time = Time.now
|
2011-09-11 16:08:26 +00:00
|
|
|
res = yield
|
2012-02-23 16:52:09 +00:00
|
|
|
if @logger && (@logger.level == DEBUG_LEVEL)
|
2012-02-16 19:00:52 +00:00
|
|
|
log_operation(name, payload, start_time)
|
|
|
|
end
|
2011-09-11 16:08:26 +00:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2012-02-16 19:00:52 +00:00
|
|
|
def log_operation(name, payload, start_time)
|
|
|
|
msg = "MONGODB "
|
|
|
|
msg << "(#{((Time.now - start_time) * 1000).to_i}ms) "
|
|
|
|
msg << "#{payload[:database]}['#{payload[:collection]}'].#{name}("
|
|
|
|
msg << payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")"
|
|
|
|
msg << ".skip(#{payload[:skip]})" if payload[:skip]
|
|
|
|
msg << ".limit(#{payload[:limit]})" if payload[:limit]
|
|
|
|
msg << ".sort(#{payload[:order]})" if payload[:order]
|
|
|
|
|
|
|
|
@logger.debug(msg)
|
2011-09-11 16:08:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|