Add full_host in logger for HTTP(s) queries

This commit is contained in:
Damien Alexandre 2013-12-09 10:40:47 +01:00
parent c97a4abceb
commit ca507a5e34
4 changed files with 30 additions and 15 deletions

View file

@ -4,6 +4,8 @@ namespace FOS\ElasticaBundle;
use Elastica\Client as ElasticaClient; use Elastica\Client as ElasticaClient;
use Elastica\Request; use Elastica\Request;
use Elastica\Transport\Http;
use Elastica\Transport\Https;
/** /**
* @author Gordon Franke <info@nevalon.de> * @author Gordon Franke <info@nevalon.de>
@ -17,7 +19,16 @@ class Client extends ElasticaClient
if (null !== $this->_logger) { if (null !== $this->_logger) {
$time = microtime(true) - $start; $time = microtime(true) - $start;
$this->_logger->logQuery($path, $method, $data, $time);
$connection = $this->getLastRequest()->getConnection();
$transport = $connection->getTransportObject();
$full_host = null;
if ($transport instanceof Http || $transport instanceof Https) {
$full_host = $connection->getTransport().'://'.$connection->getHost().':'.$connection->getPort();
}
$this->_logger->logQuery($path, $method, $data, $time, $full_host);
} }
return $response; return $response;

View file

@ -38,15 +38,17 @@ class ElasticaLogger implements LoggerInterface
* @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data arguments * @param array $data arguments
* @param float $time execution time * @param float $time execution time
* @param string $full_host host and port of the query
*/ */
public function logQuery($path, $method, $data, $time) public function logQuery($path, $method, $data, $time, $full_host = null)
{ {
if ($this->debug) { if ($this->debug) {
$this->queries[] = array( $this->queries[] = array(
'path' => $path, 'path' => $path,
'method' => $method, 'method' => $method,
'data' => $data, 'data' => $data,
'executionMS' => $time 'executionMS' => $time,
'full_host' => $full_host
); );
} }

View file

@ -52,15 +52,17 @@
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms <strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
</small> </small>
<a href="#elastica_curl_query_{{ key }}" onclick="return curl_version(this);" style="text-decoration: none;" title="Get the cURL repeatable query"> {% if query.full_host %}
<img alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="display: inline; width: 12px; height: 12px; vertical-align: bottom;" /> <a href="#elastica_curl_query_{{ key }}" onclick="return curl_version(this);" style="text-decoration: none;" title="Get the cURL repeatable query">
<img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none; width: 12px; height: 12px; vertical-align: bottom;" /> <img alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="display: inline; width: 12px; height: 12px; vertical-align: bottom;" />
<small>Display cURL query</small> <img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none; width: 12px; height: 12px; vertical-align: bottom;" />
</a> <small>Display cURL query</small>
</a>
<div style="display: none;" id="elastica_curl_query_{{ key }}"> <div style="display: none;" id="elastica_curl_query_{{ key }}">
<code>curl -X{{ query.method }} 'http://localhost:9200/{{ query.path }}' -d '{{ query.data|json_encode }}'</code> <code>curl -X{{ query.method }} '{{ query.full_host }}/{{ query.path }}' -d '{{ query.data|json_encode }}'</code>
</div> </div>
{% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
@ -79,7 +81,6 @@
} else { } else {
sections[0].style.display = 'inline'; sections[0].style.display = 'inline';
sections[1].style.display = 'none'; sections[1].style.display = 'none';
//sections[3].style.display = 'none';
document.getElementById(link.hash.replace("#", "")).style.display = 'none'; document.getElementById(link.hash.replace("#", "")).style.display = 'none';
} }

View file

@ -9,7 +9,6 @@ use FOS\ElasticaBundle\Logger\ElasticaLogger;
*/ */
class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
{ {
public function testGetZeroIfNoQueriesAdded() public function testGetZeroIfNoQueriesAdded()
{ {
$elasticaLogger = new ElasticaLogger; $elasticaLogger = new ElasticaLogger;
@ -36,15 +35,17 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
$method = 'testMethod'; $method = 'testMethod';
$data = array('data'); $data = array('data');
$time = 12; $time = 12;
$full_host = 'http://example.com:9200';
$expected = array( $expected = array(
'path' => $path, 'path' => $path,
'method' => $method, 'method' => $method,
'data' => $data, 'data' => $data,
'executionMS' => $time 'executionMS' => $time,
'full_host' => $full_host,
); );
$elasticaLogger->logQuery($path, $method, $data, $time); $elasticaLogger->logQuery($path, $method, $data, $time, $full_host);
$returnedQueries = $elasticaLogger->getQueries(); $returnedQueries = $elasticaLogger->getQueries();
$this->assertEquals($expected, $returnedQueries[0]); $this->assertEquals($expected, $returnedQueries[0]);
} }