Skip to content

Commit 8713c2d

Browse files
committed
[DoctrineBridge][DoctrineBundle] Refactored the DBAL logging
This allows enabling the logging and the profiling separately for instance when doing batch processing leading to memory issue due to the profiling.
1 parent 2750adb commit 8713c2d

File tree

11 files changed

+98
-19
lines changed

11 files changed

+98
-19
lines changed

src/Symfony/Bridge/Doctrine/DataCollector/DoctrineDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
namespace Symfony\Bridge\Doctrine\DataCollector;
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\DBAL\Logging\DebugStack;
1516
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Bridge\Doctrine\Logger\DbalLogger;
1919

2020
/**
2121
* DoctrineDataCollector.
@@ -28,7 +28,7 @@ class DoctrineDataCollector extends DataCollector
2828
private $managers;
2929
private $logger;
3030

31-
public function __construct(ManagerRegistry $registry, DbalLogger $logger = null)
31+
public function __construct(ManagerRegistry $registry, DebugStack $logger = null)
3232
{
3333
$this->connections = $registry->getConnectionNames();
3434
$this->managers = $registry->getManagerNames();

src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
use Symfony\Component\HttpKernel\Log\LoggerInterface;
1515
use Symfony\Component\HttpKernel\Debug\Stopwatch;
16-
use Doctrine\DBAL\Logging\DebugStack;
16+
use Doctrine\DBAL\Logging\SQLLogger;
1717

1818
/**
1919
* DbalLogger.
2020
*
2121
* @author Fabien Potencier <fabien@symfony.com>
2222
*/
23-
class DbalLogger extends DebugStack
23+
class DbalLogger implements SQLLogger
2424
{
2525
protected $logger;
2626
protected $stopwatch;
@@ -42,8 +42,6 @@ public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch
4242
*/
4343
public function startQuery($sql, array $params = null, array $types = null)
4444
{
45-
parent::startQuery($sql, $params, $types);
46-
4745
if (null !== $this->stopwatch) {
4846
$this->stopwatch->start('doctrine', 'doctrine');
4947
}
@@ -58,8 +56,6 @@ public function startQuery($sql, array $params = null, array $types = null)
5856
*/
5957
public function stopQuery()
6058
{
61-
parent::stopQuery();
62-
6359
if (null !== $this->stopwatch) {
6460
$this->stopwatch->stop('doctrine');
6561
}

src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private function addDbalSection(ArrayNodeDefinition $node)
7878
'platform_service',
7979
'charset',
8080
'logging',
81+
'profiling',
8182
'mapping_types',
8283
) as $key) {
8384
if (array_key_exists($key, $v)) {
@@ -130,6 +131,7 @@ private function getDbalConnectionsNode()
130131
->scalarNode('platform_service')->end()
131132
->scalarNode('charset')->end()
132133
->booleanNode('logging')->defaultValue($this->debug)->end()
134+
->booleanNode('profiling')->defaultValue($this->debug)->end()
133135
->scalarNode('driver_class')->end()
134136
->scalarNode('wrapper_class')->end()
135137
->arrayNode('options')

src/Symfony/Bundle/DoctrineBundle/DependencyInjection/DoctrineExtension.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,18 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
9292
{
9393
// configuration
9494
$configuration = $container->setDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name), new DefinitionDecorator('doctrine.dbal.connection.configuration'));
95-
if (isset($connection['logging']) && $connection['logging']) {
96-
$configuration->addMethodCall('setSQLLogger', array(new Reference('doctrine.dbal.logger')));
97-
unset ($connection['logging']);
95+
$logger = null;
96+
if ($connection['logging']) {
97+
$logger = new Reference('doctrine.dbal.logger');
98+
}
99+
unset ($connection['logging']);
100+
if ($connection['profiling']) {
101+
$logger = $logger ? new Reference('doctrine.dbal.logger.chain') : new Reference('doctrine.dbal.logger.profiling');
102+
}
103+
unset($connection['profiling']);
104+
105+
if ($logger) {
106+
$configuration->addMethodCall('setSQLLogger', array($logger));
98107
}
99108

100109
// event manager

src/Symfony/Bundle/DoctrineBundle/Resources/config/dbal.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="doctrine.dbal.logger.debug.class">Doctrine\DBAL\Logging\DebugStack</parameter>
8+
<parameter key="doctrine.dbal.logger.chain.class">Doctrine\DBAL\Logging\LoggerChain</parameter>
9+
<parameter key="doctrine.dbal.logger.profiling.class">Doctrine\DBAL\Logging\DebugStack</parameter>
910
<parameter key="doctrine.dbal.logger.class">Symfony\Bridge\Doctrine\Logger\DbalLogger</parameter>
1011
<parameter key="doctrine.dbal.configuration.class">Doctrine\DBAL\Configuration</parameter>
1112
<parameter key="doctrine.data_collector.class">Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector</parameter>
@@ -19,7 +20,16 @@
1920
</parameters>
2021

2122
<services>
22-
<service id="doctrine.dbal.logger.debug" class="%doctrine.dbal.logger.debug.class%" public="false" />
23+
<service id="doctrine.dbal.logger.chain" class="%doctrine.dbal.logger.chain.class%" public="false">
24+
<call method="addLogger">
25+
<argument type="service" id="doctrine.dbal.logger" />
26+
</call>
27+
<call method="addLogger">
28+
<argument type="service" id="doctrine.dbal.logger.profiling" />
29+
</call>
30+
</service>
31+
32+
<service id="doctrine.dbal.logger.profiling" class="%doctrine.dbal.logger.profiling.class%" public="false" />
2333

2434
<service id="doctrine.dbal.logger" class="%doctrine.dbal.logger.class%" public="false">
2535
<tag name="monolog.logger" channel="doctrine" />
@@ -30,7 +40,7 @@
3040
<service id="data_collector.doctrine" class="%doctrine.data_collector.class%" public="false">
3141
<tag name="data_collector" template="DoctrineBundle:Collector:db" id="db" />
3242
<argument type="service" id="doctrine" />
33-
<argument type="service" id="doctrine.dbal.logger" />
43+
<argument type="service" id="doctrine.dbal.logger.profiling" />
3444
</service>
3545

3646
<service id="doctrine.dbal.connection_factory" class="%doctrine.dbal.connection_factory.class%">

src/Symfony/Bundle/DoctrineBundle/Resources/config/schema/doctrine-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<xsd:attribute name="wrapper-class" type="xsd:string" />
3030
<xsd:attribute name="platform-service" type="xsd:string" />
3131
<xsd:attribute name="logging" type="xsd:string" default="false" />
32+
<xsd:attribute name="profiling" type="xsd:string" default="false" />
3233
</xsd:attributeGroup>
3334

3435
<xsd:complexType name="dbal">

src/Symfony/Bundle/DoctrineBundle/Tests/ContainerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ class ContainerTest extends TestCase
1818
public function testContainer()
1919
{
2020
$container = $this->createYamlBundleTestContainer();
21-
$this->assertInstanceOf('Doctrine\DBAL\Logging\DebugStack', $container->get('doctrine.dbal.logger.debug'));
22-
$this->assertInstanceOf('Doctrine\DBAL\Logging\DebugStack', $container->get('doctrine.dbal.logger'));
21+
$this->assertInstanceOf('Doctrine\DBAL\Logging\DebugStack', $container->get('doctrine.dbal.logger.profiling'));
22+
$this->assertInstanceOf('Doctrine\DBAL\Logging\LoggerChain', $container->get('doctrine.dbal.logger.chain'));
23+
$this->assertInstanceOf('Symfony\Bridge\Doctrine\Logger\DbalLogger', $container->get('doctrine.dbal.logger'));
2324
$this->assertInstanceOf('Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector', $container->get('data_collector.doctrine'));
2425
$this->assertInstanceOf('Doctrine\DBAL\Configuration', $container->get('doctrine.dbal.default_connection.configuration'));
2526
$this->assertInstanceOf('Doctrine\Common\EventManager', $container->get('doctrine.dbal.default_connection.event_manager'));

src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ public function testLoadSimpleSingleConnection()
218218
'user' => 'root',
219219
'password' => null,
220220
'driver' => 'pdo_mysql',
221-
'logging' => false,
222221
'driverOptions' => array(),
223222
),
224223
new Reference('doctrine.dbal.default_connection.configuration'),
@@ -258,7 +257,6 @@ public function testLoadSingleConnection()
258257
'password' => 'sqlite_s3cr3t',
259258
'dbname' => 'sqlite_db',
260259
'memory' => true,
261-
'logging' => false,
262260
),
263261
new Reference('doctrine.dbal.default_connection.configuration'),
264262
new Reference('doctrine.dbal.default_connection.event_manager'),
@@ -337,6 +335,26 @@ public function testLoadMultipleConnections()
337335
$this->assertEquals('%doctrine.orm.cache.array.class%', $definition->getClass());
338336
}
339337

338+
public function testLoadLogging()
339+
{
340+
$container = $this->getContainer();
341+
$loader = new DoctrineExtension();
342+
$container->registerExtension($loader);
343+
344+
$this->loadFromFile($container, 'dbal_logging');
345+
346+
$this->compileContainer($container);
347+
348+
$definition = $container->getDefinition('doctrine.dbal.log_connection.configuration');
349+
$this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', array(new Reference('doctrine.dbal.logger')));
350+
351+
$definition = $container->getDefinition('doctrine.dbal.profile_connection.configuration');
352+
$this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', array(new Reference('doctrine.dbal.logger.profiling')));
353+
354+
$definition = $container->getDefinition('doctrine.dbal.both_connection.configuration');
355+
$this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', array(new Reference('doctrine.dbal.logger.chain')));
356+
}
357+
340358
public function testBundleEntityAliases()
341359
{
342360
$container = $this->getContainer();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" ?>
2+
3+
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:srv="http://symfony.com/schema/dic/services"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
8+
9+
<config>
10+
<dbal default-connection="mysql">
11+
<connection
12+
name="log"
13+
logging="true"
14+
profiling="false" />
15+
<connection
16+
name="profile"
17+
logging="false"
18+
profiling="true" />
19+
<connection
20+
name="both"
21+
logging="true"
22+
profiling="true" />
23+
<connection
24+
name="none"
25+
logging="false"
26+
profiling="false" />
27+
</dbal>
28+
</config>
29+
</srv:container>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
doctrine:
2+
dbal:
3+
default_connection: mysql
4+
connections:
5+
log:
6+
logging: true
7+
profiling: false
8+
profile:
9+
logging: false
10+
profiling: true
11+
both:
12+
logging: true
13+
profiling: true

0 commit comments

Comments
 (0)