Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stopped logger collecting data when not in debug mode

  • Loading branch information...
commit 16f439cfaf3275b36f294abeb001f613a6b7f77f 1 parent 3bc34c1
@richardmiller richardmiller authored
View
18 Logger/ElasticaLogger.php
@@ -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 @@ public function __construct(LoggerInterface $logger = null)
*/
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
1  Resources/config/config.xml
@@ -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
16 Tests/Logger/ElasticaLoggerTest.php
@@ -18,7 +18,7 @@ public function testGetZeroIfNoQueriesAdded()
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 @@ public function testCorrectAmountIfRandomNumberOfQueriesAdded()
public function testCorrectlyFormattedQueryReturned()
{
- $elasticaLogger = new ElasticaLogger;
+ $elasticaLogger = new ElasticaLogger(null, true);
$path = 'testPath';
$method = 'testMethod';
@@ -49,6 +49,18 @@ public function testCorrectlyFormattedQueryReturned()
$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')
Please sign in to comment.
Something went wrong with that request. Please try again.