lib/mongo/util/logging.rb in mongo-1.7.1 vs lib/mongo/util/logging.rb in mongo-1.8.0
- old
+ new
@@ -27,27 +27,43 @@
end
# Execute the block and log the operation described by name and payload.
def instrument(name, payload = {})
start_time = Time.now
- res = yield
- log_operation(name, payload, start_time)
+ res = Logging.instrumenter.instrument(name, payload) do
+ yield
+ end
+ duration = Time.now - start_time
+ log_operation(name, payload, duration)
res
end
+ def self.instrumenter
+ @instrumenter || Instrumenter
+ end
+
+ def self.instrumenter=(instrumenter)
+ @instrumenter = instrumenter
+ end
+
protected
- def log_operation(name, payload, start_time)
+ def log_operation(name, payload, duration)
@logger && @logger.debug do
msg = "MONGODB "
- msg << "(#{((Time.now - start_time) * 1000).to_i}ms) "
+ msg << "(%.1fms) " % (duration * 1000)
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
end
+ module Instrumenter
+ def self.instrument(name, payload = {})
+ yield
+ end
+ end
end
end