Skip to content

Commit

Permalink
add execution time; remove logger preffix; add as second parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gimler committed Oct 4, 2011
1 parent 82b82b1 commit d17bd57
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
8 changes: 6 additions & 2 deletions Client.php
Expand Up @@ -18,10 +18,14 @@ public function setLogger($logger)

public function request($path, $method, $data = array())
{
$start = microtime(true);
$response = parent::request($path, $method, $data);

if (null !== $this->logger) {
$this->logger->logQuery($path, $method, $data);
$time = microtime(true) - $start;
$this->logger->logQuery($path, $method, $data, $time);
}

return parent::request($path, $method, $data);
return $response;
}
}
19 changes: 10 additions & 9 deletions Logger/ElasticaLogger.php
Expand Up @@ -15,19 +15,16 @@
class ElasticaLogger
{
protected $logger;
protected $prefix;
protected $queries;

/**
* Constructor.
*
* @param LoggerInterface $logger The Symfony logger
* @param string $prefix A prefix for messages sent to the Symfony logger
*/
public function __construct(LoggerInterface $logger = null, $prefix = 'Elastica')
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->prefix = $prefix;
$this->queries = array();
}

Expand All @@ -41,14 +38,18 @@ public function __construct(LoggerInterface $logger = null, $prefix = 'Elastica'
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data OPTIONAL Arguments as array
*/
public function logQuery($path, $method, array $data = array())
public function logQuery($path, $method, array $data, $time)
{
$logInfo = sprintf("%s: %s (%s) \n%s", $this->prefix, $path, $method, json_encode($data));

$this->queries[] = $logInfo;
$this->queries[] = array(
'path' => $path,
'method' => $method,
'data' => $data,
'executionMS' => $time
);

if (null !== $this->logger) {
$this->logger->info($logInfo);
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
$this->logger->info($message, $data);
}
}

Expand Down
7 changes: 6 additions & 1 deletion Resources/views/Collector/elastica.html.twig
Expand Up @@ -35,9 +35,14 @@
<ul class="alt">
{% for query in collector.queries %}
<li class="{{ cycle(['odd', 'even'], loop.index) }}">
<strong>Path</strong>: {{ query.path }}<br />
<strong>Method</strong>: {{ query.method }}
<div>
<code>{{ query }}</code>
<code>{{ query.data|json_encode }}</code>
</div>
<small>
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
</small>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit d17bd57

Please sign in to comment.