From 4f49812acbfbd2465469b6b20fbeac9e5c516dd2 Mon Sep 17 00:00:00 2001 From: Erez Rabih Date: Wed, 4 Dec 2013 14:04:34 +0200 Subject: [PATCH] basic query logging --- lib/neography/connection.rb | 20 ++++++++++++++++++-- spec/unit/connection_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index f3b9d72..193118f 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -40,11 +40,27 @@ def merge_options(options) ACTIONS.each do |action| define_method(action) do |path, options = {}| - authenticate(configuration + path) - evaluate_response(@client.send(action.to_sym, configuration + path, merge_options(options)[:body], merge_options(options)[:headers])) + query_path = configuration + path + query_body = merge_options(options)[:body] + authenticate(query_path) + log path, query_body do + evaluate_response(@client.send(action.to_sym, query_path, query_body, merge_options(options)[:headers])) + end end end + def log(path, body) + if @log_enabled + start_time = Time.now + response = yield + time = ((Time.now - start_time) * 1000).round(2) + @logger.info "[Neography::Query] #{path} #{body} [#{time}ms]" + response + else + yield + end + end + def authenticate(path) @client.set_auth(path, @authentication[@authentication.keys.first][:username], diff --git a/spec/unit/connection_spec.rb b/spec/unit/connection_spec.rb index 0964b17..567a6e3 100644 --- a/spec/unit/connection_spec.rb +++ b/spec/unit/connection_spec.rb @@ -204,6 +204,31 @@ module Neography end end + + context "query logging" do + before do + connection.logger = Logger.new(nil) + connection.log_enabled = true + end + + let :expected_response do + "expected_response" + end + + let :request_body do + {key1: :val1} + end + + it "should log query" do + connection.should_receive(:log).with("/foo/bar", request_body).once + connection.get("/foo/bar", {body: request_body}) + end + + it "should return original response" do + connection.stub(:evaluate_response).and_return expected_response + connection.get("/foo/bar").should eq expected_response + end + end end end end