2011-09-11 16:08:26 +00:00
|
|
|
module Mongo
|
|
|
|
module Logging
|
|
|
|
|
|
|
|
# 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 :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
|
|
|
|
@logger.fatal "MONGODB [FATAL] #{msg}"
|
|
|
|
else
|
|
|
|
@logger.info "MONGODB [INFO] #{msg}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Execute the block and log the operation described by name and payload.
|
|
|
|
def instrument(name, payload = {}, &blk)
|
|
|
|
res = yield
|
|
|
|
log_operation(name, payload)
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def log_operation(name, payload)
|
|
|
|
@logger ||= nil
|
|
|
|
return unless @logger
|
|
|
|
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 "MONGODB #{msg}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|