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

Skip to content

Commit 5271fb6

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

File tree

7 files changed

+167
-3
lines changed

7 files changed

+167
-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
54
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function countErrors()
5252
*/
5353
private function getDebugLogger()
54
{
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: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
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+
/**
23+
* @group legacy
24+
*/
2025
public function testGetLogsWithDebugHandler()
2126
{
2227
$handler = new DebugHandler();
@@ -26,7 +31,7 @@ public function testGetLogsWithDebugHandler()
2631
$this->assertSame(1, count($logger->getLogs()));
2732
}
2833

29-
public function testGetLogsWithoutDebugHandler()
34+
public function testGetLogsWithoutDebugProcessor()
3035
{
3136
$handler = new TestHandler();
3237
$logger = new Logger(__METHOD__, array($handler));
@@ -35,6 +40,9 @@ public function testGetLogsWithoutDebugHandler()
3540
$this->assertSame(array(), $logger->getLogs());
3641
}
3742

43+
/**
44+
* @group legacy
45+
*/
3846
public function testCountErrorsWithDebugHandler()
3947
{
4048
$handler = new DebugHandler();
@@ -53,7 +61,10 @@ public function testCountErrorsWithDebugHandler()
5361
$this->assertSame(4, $logger->countErrors());
5462
}
5563

56-
public function testGetLogs()
64+
/**
65+
* @group legacy
66+
*/
67+
public function testGetLogsWithDebugHandler2()
5768
{
5869
$logger = new Logger('test');
5970
$logger->pushHandler(new DebugHandler());
@@ -66,12 +77,55 @@ public function testGetLogs()
6677
$this->assertEquals(Logger::INFO, $record['priority']);
6778
}
6879

69-
public function testCountErrorsWithoutDebugHandler()
80+
public function testCountErrorsWithoutDebugProcessor()
7081
{
7182
$handler = new TestHandler();
7283
$logger = new Logger(__METHOD__, array($handler));
7384

7485
$this->assertTrue($logger->error('error message'));
7586
$this->assertSame(0, $logger->countErrors());
7687
}
88+
89+
public function testGetLogsWithDebugProcessor()
90+
{
91+
$handler = new TestHandler();
92+
$processor = new DebugProcessor();
93+
$logger = new Logger(__METHOD__, array($handler), array($processor));
94+
95+
$this->assertTrue($logger->error('error message'));
96+
$this->assertSame(1, count($logger->getLogs()));
97+
}
98+
99+
public function testCountErrorsWithDebugProcessor()
100+
{
101+
$handler = new TestHandler();
102+
$processor = new DebugProcessor();
103+
$logger = new Logger(__METHOD__, array($handler), array($processor));
104+
105+
$this->assertTrue($logger->debug('test message'));
106+
$this->assertTrue($logger->info('test message'));
107+
$this->assertTrue($logger->notice('test message'));
108+
$this->assertTrue($logger->warning('test message'));
109+
110+
$this->assertTrue($logger->error('test message'));
111+
$this->assertTrue($logger->critical('test message'));
112+
$this->assertTrue($logger->alert('test message'));
113+
$this->assertTrue($logger->emergency('test message'));
114+
115+
$this->assertSame(4, $logger->countErrors());
116+
}
117+
118+
public function testGetLogsWithDebugProcessor2()
119+
{
120+
$handler = new TestHandler();
121+
$logger = new Logger('test', array($handler));
122+
$logger->pushProcessor(new DebugProcessor());
123+
124+
$logger->addInfo('test');
125+
$this->assertCount(1, $logger->getLogs());
126+
list($record) = $logger->getLogs();
127+
128+
$this->assertEquals('test', $record['message']);
129+
$this->assertEquals(Logger::INFO, $record['priority']);
130+
}
77131
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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('monolog.logger_prototype')) {
23+
return;
24+
}
25+
26+
if (!$container->hasDefinition('debug.log_processor')) {
27+
return;
28+
}
29+
30+
$definition = $container->getDefinition('monolog.logger_prototype');
31+
$definition->addMethodCall('pushProcessor', array(new Reference('debug.log_processor')));
32+
}
33+
}

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