From a7ec07a957a348c8edbfc7cec1b856657bd40f5e Mon Sep 17 00:00:00 2001 From: Cyril Mougel Date: Thu, 10 Sep 2009 23:24:06 +0200 Subject: [PATCH] implement a little logger with log time and query --- lib/mongo/connection.rb | 2 +- lib/mongo/db.rb | 6 +++++- lib/mongo/query.rb | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index fcbc021..c131e1d 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -82,7 +82,7 @@ module Mongo when nil [['localhost', DEFAULT_PORT]] end - @options = options + @options = options.merge(:logger => Logger.new(STDOUT)) end # Return the Mongo::DB named +db_name+. The slave_ok and diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 31ebc22..5cc6c6d 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -135,6 +135,7 @@ module Mongo @semaphore = Object.new @semaphore.extend Mutex_m @socket = nil + @logger = options[:logger] connect_to_master end @@ -512,8 +513,11 @@ module Mongo def send_to_db(message) connect_to_master if !connected? && @auto_reconnect begin + t_start = Time.now @socket.print(message.buf.to_s) - @socket.flush + res = @socket.flush + @logger.debug(" MONGODB (#{Time.now - t_start}s) #{message.query}") + res rescue => ex close raise ex diff --git a/lib/mongo/query.rb b/lib/mongo/query.rb index 4d85826..b7014bf 100644 --- a/lib/mongo/query.rb +++ b/lib/mongo/query.rb @@ -111,5 +111,9 @@ module Mongo def contains_special_fields (@order_by != nil && @order_by.length > 0) || @explain || @hint || @snapshot end + + def to_s + "#find(#{@selector.inspect})" + (@order_by ? ".sort(#{@order_by.inspect})" : "") + end end end