10000 [Bridge\Monolog][FrameworkBundle] Add & wire a DebugProcessor · symfony/symfony@7572a53 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7572a53

Browse files
[Bridge\Monolog][FrameworkBundle] Add & wire a DebugProcessor
1 parent 31d5fff commit 7572a53

File tree

7 files changed

+168
-3
lines changed

7 files changed

+168
-3
lines changed

src/Symfony/Bridge/Monolog/Handler/DebugHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\Monolog\Handler;
1313

14+
@trigger_error('The '.__NAMESPACE__.'\DebugHandler class is deprecated since version 3.2 and will be removed in 4.0. Use Symfony\Bridge\Monolog\Processor\DebugProcessor instead.', E_USER_DEPRECATED);
15+
1416
use Monolog\Logger;
1517
use Monolog\Handler\TestHandler;
1618
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
@@ -19,6 +21,8 @@
1921
* DebugLogger.
2022
*
2123
* @author Jordi Boggiano <j.boggiano@seld.be>
24+
*
25+
* @deprecated since version 3.2, to be removed in 4.0. Use Symfony\Bridge\Monolog\Processor\DebugProcessor instead.
2226
*/
2327
class DebugHandler extends TestHandler implements DebugLoggerInterface
2428
{

src/Symfony/Bridge/Monolog/Logger.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function countErrors()
5252
*/
5353
private function getDebugLogger()
5454
{
55+
foreach ($this->processors as $processor) {
56+
if ($processor instanceof DebugLoggerInterface) {
57+
return $processor;
58+
}
59+
}
60+
5561
foreach ($this->handlers as $handler) {
5662
if ($handler instanceof DebugLoggerInterface) {
5763
return $handler;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Processor;
13+
14+
use Monolog\Logger;
15+
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
16+
17+
class DebugProcessor implements DebugLoggerInterface
18+
{
19+
private $records = array();
20+
private $errorCount = 0;
21+
22+
public function __invoke(array $record)
23+
{
24+
$this->records[] = array(
25+
'timestamp' => $record['datetime']->getTimestamp(),
26+
'message' => $record['message'],
27+
'priority' => $record['level'],
28+
'priorityName' => $record['level_name'],
29+
'context' => $record['context'],
30+
'channel' => isset($record['channel']) ? $record['channel'] : '',
31+
);
32+
switch ($record['level']) {
33+
case Logger::ERROR:
34+
case Logger::CRITICAL:
35+
case Logger::ALERT:
36+
case Logger::EMERGENCY:
37+
++$this->errorCount;
38+
}
39+
40+
return $record;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function getLogs()
47+
{
48+
return $this->records;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function countErrors()
55+
{
56+
return $this->errorCount;
57+
}
58+
}

src/Symfony/Bridge/Monolog/Tests/LoggerTest.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313

1414
use Monolog\Handler\TestHandler;
1515
use Symfony\Bridge\Monolog\Handler\DebugHandler;
16+
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
1617
use Symfony\Bridge\Monolog\Logger;
1718

1819
class LoggerTest extends \PHPUnit_Framework_TestCase
1920
{
21+
/**
22+
* @group legacy
23+
*/
2024
public function testGetLogsWithDebugHandler()
2125
{
2226
$handler = new DebugHandler();
@@ -26,7 +30,7 @@ public function testGetLogsWithDebugHandler()
2630
$this->assertSame(1, count($logger->getLogs()));
2731
}
2832

29-
public function testGetLogsWithoutDebugHandler()
33+
public function testGetLogsWithoutDebugProcessor()
3034
{
3135
$handler = new TestHandler();
3236
$logger = new Logger(__METHOD__, array($handler));
@@ -35,6 +39,9 @@ public function testGetLogsWithoutDebugHandler()
3539
$this->assertSame(array(), $logger->getLogs());
3640
}
3741

42+
/**
43+
* @group legacy
44+
*/
3845
public function testCountErrorsWithDebugHandler()
3946
{
4047
$handler = new DebugHandler();
@@ -53,7 +60,10 @@ public function testCountErrorsWithDebugHandler()
5360
$this->assertSame(4, $logger->countErrors());
5461
}
5562

56-
public function testGetLogs()
63+
/**
64+
* @group legacy
65+
*/
66+
public function testGetLogsWithDebugHandler2()
5767
{
5868
$logger = new Logger('test');
5969
$logger->pushHandler(new DebugHandler());
@@ -66,12 +76,55 @@ public function testGetLogs()
6676
$this->assertEquals(Logger::INFO, $record['priority']);
6777
}
6878

69-
public function testCountErrorsWithoutDebugHandler()
79+
public function testCountErrorsWithoutDebugProcessor()
7080
{
7181
$handler = new TestHandler();
1C6A
7282
$logger = new Logger(__METHOD__, array($handler));
7383

7484
$this->assertTrue($logger->error('error message'));
7585
$this->assertSame(0, $logger->countErrors());
7686
}
87+
88+
public function testGetLogsWithDebugProcessor()
89+
{
90+
$handler = new TestHandler();
91+
$processor = new DebugProcessor();
92+
$logger = new Logger(__METHOD__, array($handler), array($processor));
93+
94+
$this->assertTrue($logger->error('error message'));
95+
$this->assertSame(1, count($logger->getLogs()));
96+
}
97+
98+
public function testCountErrorsWithDebugProcessor()
99+
{
100+
$handler = new TestHandler();
101+
$processor = new DebugProcessor();
102+
$logger = new Logger(__METHOD__, array($handler), array($processor));
103+
104+
$this->assertTrue($logger->debug('test message'));
105+
$this->assertTrue($logger->info('test message'));
106+
$this->assertTrue($logger->notice('test message'));
107+
$this->assertTrue($logger->warning('test message'));
108+
109+
$this->assertTrue($logger->error('test message'));
110+
$this->assertTrue($logger->critical('test message'));
111+
$this->assertTrue($logger->alert('test message'));
112+
$this->assertTrue($logger->emergency('test message'));
113+
114+
$this->assertSame(4, $logger->countErrors());
115+
}
116+
117+
public function testGetLogsWithDebugProcessor2()
118+
{
119+
$handler = new TestHandler();
120+
$logger = new Logger('test', array($handler));
121+
$logger->pushProcessor(new DebugProcessor());
122+
123+
$logger->addInfo('test');
124+
$this->assertCount(1, $logger->getLogs());
125+
list($record) = $logger->getLogs();
126+
127+
$this->assertEquals('test', $record['message']);
128+
$this->assertEquals(Logger::INFO, $record['priority']);
129+
}
77130
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
class AddDebugLogProcessorPass implements CompilerPassInterface
19+
{
20+
public function process(ContainerBuilder $container)
21+
{
22+
if (!$container->hasDefinition('profiler')) {
23+
return;
24+
}
25+
if (!$container->hasDefinition('monolog.logger_prototype')) {
26+
return;
27+
}
28+
if (!$container->hasDefinition('debug.log_processor')) {
29+
return;
30+
}
31+
32+
$definition = $container->getDefinition('monolog.logger_prototype');
33+
$definition->addMethodCall('pushProcessor', array(new Reference('debug.log_processor')));
34+
}
35+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

1414
use Doctrine\Common\Annotations\Reader;
15+
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
1516
use Symfony\Component\Cache\Adapter\AdapterInterface;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -468,6 +469,12 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
468469

469470
$definition->replaceArgument(4, $debug);
470471
$definition->replaceArgument(6, $debug);
472+
473+
if ($debug && class_exists(DebugProcessor::class)) {
474+
$definition = new Definition(DebugProcessor::class);
475+
$definition->setPublic(false);
476+
$container->setDefinition('debug.log_processor', $definition);
477+
}
471478
}
472479

473480
/**

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle;
1313

1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
15+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
1516
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
1617
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
1718
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
@@ -95,6 +96,7 @@ public function build(ContainerBuilder $container)
9596
$container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
9697

9798
if ($container->getParameter('kernel.debug')) {
99+
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1);
98100
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
99101
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
100102
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);

0 commit comments

Comments
 (0)
0