Stopped logger collecting data when not in debug mode

This commit is contained in:
Richard Miller 2012-08-07 19:39:14 +01:00
commit 16f439cfaf
3 changed files with 26 additions and 9 deletions

View file

@ -16,16 +16,18 @@ class ElasticaLogger
{
protected $logger;
protected $queries;
protected $debug;
/**
* Constructor.
*
* @param LoggerInterface $logger The Symfony logger
*/
public function __construct(LoggerInterface $logger = null)
public function __construct(LoggerInterface $logger = null, $debug = false)
{
$this->logger = $logger;
$this->queries = array();
$this->debug = $debug;
}
/**
@ -38,12 +40,14 @@ class ElasticaLogger
*/
public function logQuery($path, $method, $data, $time)
{
$this->queries[] = array(
'path' => $path,
'method' => $method,
'data' => $data,
'executionMS' => $time
);
if ($this->debug) {
$this->queries[] = array(
'path' => $path,
'method' => $method,
'data' => $data,
'executionMS' => $time
);
}
if (null !== $this->logger) {
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);

View file

@ -18,6 +18,7 @@
<service id="foq_elastica.logger" class="%foq_elastica.logger.class%">
<argument type="service" id="logger" on-invalid="null" />
<argument>%kernel.debug%</argument>
<tag name="monolog.logger" channel="elastica" />
</service>
<service id="foq_elastica.data_collector" class="%foq_elastica.data_collector.class%" public="true">

View file

@ -18,7 +18,7 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
public function testCorrectAmountIfRandomNumberOfQueriesAdded()
{
$elasticaLogger = new ElasticaLogger;
$elasticaLogger = new ElasticaLogger(null, true);
$total = rand(1, 15);
for ($i = 0; $i < $total; $i++) {
@ -30,7 +30,7 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
public function testCorrectlyFormattedQueryReturned()
{
$elasticaLogger = new ElasticaLogger;
$elasticaLogger = new ElasticaLogger(null, true);
$path = 'testPath';
$method = 'testMethod';
@ -49,6 +49,18 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $returnedQueries[0]);
}
public function testNoQueriesStoredIfDebugFalseAdded()
{
$elasticaLogger = new ElasticaLogger(null, false);
$total = rand(1, 15);
for ($i = 0; $i < $total; $i++) {
$elasticaLogger->logQuery('testPath', 'testMethod', array('data'), 12);
}
$this->assertEquals(0, $elasticaLogger->getNbQueries());
}
public function testQueryIsLogged()
{
$loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface')