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\Request;
use Elastica\Transport\Http;
use Elastica\Transport\Https;
/**
* @author Gordon Franke <info@nevalon.de>
@ -17,7 +19,16 @@ class Client extends ElasticaClient
if (null !== $this->_logger) {
$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;

View file

@ -38,15 +38,17 @@ class ElasticaLogger implements LoggerInterface
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data arguments
* @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) {
$this->queries[] = array(
'path' => $path,
'method' => $method,
'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
</small>
<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_more.gif') }}" style="display: inline; width: 12px; height: 12px; vertical-align: bottom;" />
<img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none; width: 12px; height: 12px; vertical-align: bottom;" />
<small>Display cURL query</small>
</a>
{% if query.full_host %}
<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_more.gif') }}" style="display: inline; width: 12px; height: 12px; vertical-align: bottom;" />
<img alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="display: none; width: 12px; height: 12px; vertical-align: bottom;" />
<small>Display cURL query</small>
</a>
<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>
</div>
<div style="display: none;" id="elastica_curl_query_{{ key }}">
<code>curl -X{{ query.method }} '{{ query.full_host }}/{{ query.path }}' -d '{{ query.data|json_encode }}'</code>
</div>
{% endif %}
</li>
{% endfor %}
</ul>
@ -79,7 +81,6 @@
} else {
sections[0].style.display = 'inline';
sections[1].style.display = 'none';
//sections[3].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
{
public function testGetZeroIfNoQueriesAdded()
{
$elasticaLogger = new ElasticaLogger;
@ -36,15 +35,17 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
$method = 'testMethod';
$data = array('data');
$time = 12;
$full_host = 'http://example.com:9200';
$expected = array(
'path' => $path,
'method' => $method,
'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();
$this->assertEquals($expected, $returnedQueries[0]);
}