Skip to content

Commit

Permalink
Stopped logger collecting data when not in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmiller-zz committed Aug 7, 2012
1 parent 3bc34c1 commit 16f439c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
18 changes: 11 additions & 7 deletions Logger/ElasticaLogger.php
Expand Up @@ -16,16 +16,18 @@ class ElasticaLogger
{ {
protected $logger; protected $logger;
protected $queries; protected $queries;
protected $debug;


/** /**
* Constructor. * Constructor.
* *
* @param LoggerInterface $logger The Symfony logger * @param LoggerInterface $logger The Symfony logger
*/ */
public function __construct(LoggerInterface $logger = null) public function __construct(LoggerInterface $logger = null, $debug = false)
{ {
$this->logger = $logger; $this->logger = $logger;
$this->queries = array(); $this->queries = array();
$this->debug = $debug;
} }


/** /**
Expand All @@ -38,12 +40,14 @@ public function __construct(LoggerInterface $logger = null)
*/ */
public function logQuery($path, $method, $data, $time) public function logQuery($path, $method, $data, $time)
{ {
$this->queries[] = array( if ($this->debug) {
'path' => $path, $this->queries[] = array(
'method' => $method, 'path' => $path,
'data' => $data, 'method' => $method,
'executionMS' => $time 'data' => $data,
); 'executionMS' => $time
);
}


if (null !== $this->logger) { if (null !== $this->logger) {
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000); $message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
Expand Down
1 change: 1 addition & 0 deletions Resources/config/config.xml
Expand Up @@ -18,6 +18,7 @@


<service id="foq_elastica.logger" class="%foq_elastica.logger.class%"> <service id="foq_elastica.logger" class="%foq_elastica.logger.class%">
<argument type="service" id="logger" on-invalid="null" /> <argument type="service" id="logger" on-invalid="null" />
<argument>%kernel.debug%</argument>
<tag name="monolog.logger" channel="elastica" /> <tag name="monolog.logger" channel="elastica" />
</service> </service>
<service id="foq_elastica.data_collector" class="%foq_elastica.data_collector.class%" public="true"> <service id="foq_elastica.data_collector" class="%foq_elastica.data_collector.class%" public="true">
Expand Down
16 changes: 14 additions & 2 deletions Tests/Logger/ElasticaLoggerTest.php
Expand Up @@ -18,7 +18,7 @@ public function testGetZeroIfNoQueriesAdded()


public function testCorrectAmountIfRandomNumberOfQueriesAdded() public function testCorrectAmountIfRandomNumberOfQueriesAdded()
{ {
$elasticaLogger = new ElasticaLogger; $elasticaLogger = new ElasticaLogger(null, true);


$total = rand(1, 15); $total = rand(1, 15);
for ($i = 0; $i < $total; $i++) { for ($i = 0; $i < $total; $i++) {
Expand All @@ -30,7 +30,7 @@ public function testCorrectAmountIfRandomNumberOfQueriesAdded()


public function testCorrectlyFormattedQueryReturned() public function testCorrectlyFormattedQueryReturned()
{ {
$elasticaLogger = new ElasticaLogger; $elasticaLogger = new ElasticaLogger(null, true);


$path = 'testPath'; $path = 'testPath';
$method = 'testMethod'; $method = 'testMethod';
Expand All @@ -49,6 +49,18 @@ public function testCorrectlyFormattedQueryReturned()
$this->assertEquals($expected, $returnedQueries[0]); $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() public function testQueryIsLogged()
{ {
$loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface') $loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface')
Expand Down

0 comments on commit 16f439c

Please sign in to comment.