2011-09-11 16:08:26 +00:00
|
|
|
module Mongo
|
|
|
|
module Logging
|
|
|
|
|
2011-11-15 20:59:42 +00:00
|
|
|
def write_logging_startup_message
|
2012-04-27 07:26:49 +00:00
|
|
|
log(:debug, "Logging level is currently :debug which could negatively impact " +
|
2012-03-07 18:23:00 +00:00
|
|
|
"client-side performance. You should set your logging level no lower than " +
|
|
|
|
":info in production.")
|
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
|
2012-06-01 19:16:05 +00:00
|
|
|
@logger.fatal "MONGODB [FATAL] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
when :error then
|
2012-06-01 19:16:05 +00:00
|
|
|
@logger.error "MONGODB [ERROR] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
when :warn then
|
2012-06-01 19:16:05 +00:00
|
|
|
@logger.warn "MONGODB [WARNING] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
when :info then
|
2012-06-01 19:16:05 +00:00
|
|
|
@logger.info "MONGODB [INFO] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
when :debug then
|
2012-06-01 19:16:05 +00:00
|
|
|
@logger.debug "MONGODB [DEBUG] #{msg}"
|
2012-02-16 19:00:52 +00:00
|
|
|
else
|
2012-06-01 19:16:05 +00:00
|
|
|
@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.
|
2012-04-27 07:26:49 +00:00
|
|
|
def instrument(name, payload = {})
|
2012-02-16 19:00:52 +00:00
|
|
|
start_time = Time.now
|
2011-09-11 16:08:26 +00:00
|
|
|
res = yield
|
2012-04-27 07:26:49 +00:00
|
|
|
log_operation(name, payload, start_time)
|
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)
|
2012-04-27 07:26:49 +00:00
|
|
|
@logger && @logger.debug do
|
|
|
|
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]
|
|
|
|
msg
|
|
|
|
end
|
2011-09-11 16:08:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|