8000 [Bridge\Monolog][FrameworkBundle] Add & wire a DebugProcessor by nicolas-grekas · Pull Request #20416 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Bridge\Monolog][FrameworkBundle] Add & wire a DebugProcessor #20416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Bridge\Monolog][FrameworkBundle] Add & wire a DebugProcessor
  • Loading branch information
nicolas-grekas committed Nov 5, 2016
commit 7572a53b847af0c74ec1c23cd56087c46f9a2978
4 changes: 4 additions & 0 deletions src/Symfony/Bridge/Monolog/Handler/DebugHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bridge\Monolog\Handler;

@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);

use Monolog\Logger;
use Monolog\Handler\TestHandler;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
Expand All @@ -19,6 +21,8 @@
* DebugLogger.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @deprecated since version 3.2, to be removed in 4.0. Use Symfony\Bridge\Monolog\Processor\DebugProcessor instead.
*/
class DebugHandler extends TestHandler implements DebugLoggerInterface
{
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function countErrors()
*/
private function getDebugLogger()
{
foreach ($this->processors as $processor) {
if ($processor instanceof DebugLoggerInterface) {
return $processor;
}
}

foreach ($this->handlers as $handler) {
if ($handler instanceof DebugLoggerInterface) {
return $handler;
Expand Down
58 changes: 58 additions & 0 deletions src/Symfony/Bridge/Monolog/Processor/DebugProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Processor;

use Monolog\Logger;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

class DebugProcessor implements DebugLoggerInterface
{
private $records = array();
private $errorCount = 0;

public function __invoke(array $record)
{
$this->records[] = array(
'timestamp' => $record['datetime']->getTimestamp(),
'message' => $record['message'],
'priority' => $record['level'],
'priorityName' => $record['level_name'],
'context' => $record['context'],
'channel' => isset($record['channel']) ? $record['channel'] : '',
);
switch ($record['level']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switch looks unneeded. What about using a simple if?

if (in_array($record['level'], array(Logger::ERROR, Logger::CRITICAL, Logger::ALERT, Logger::EMERGENCY))) {
    ++$this->errorCount;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer line, one array allocation, one function call: not my preference...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK then ... but according to 3v4l.org, the if code generates 7 opcodes and the switch code generates 11 opcodes (although it's true that not all opcodes are equal!)

case Logger::ERROR:
case Logger::CRITICAL:
case Logger::ALERT:
case Logger::EMERGENCY:
++$this->errorCount;
}

return $record;
}

/**
* {@inheritdoc}
*/
public function getLogs()
{
return $this->records;
}

/**
* {@inheritdoc}
*/
public function countErrors()
{
return $this->errorCount;
}
}
59 changes: 56 additions & 3 deletions src/Symfony/Bridge/Monolog/Tests/LoggerTest.php
EDBE
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@

use Monolog\Handler\TestHandler;
use Symfony\Bridge\Monolog\Handler\DebugHandler;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Bridge\Monolog\Logger;

class LoggerTest extends \PHPUnit_Framework_TestCase
{
/**
* @group legacy
*/
public function testGetLogsWithDebugHandler()
{
$handler = new DebugHandler();
Expand All @@ -26,7 +30,7 @@ public function testGetLogsWithDebugHandler()
$this->assertSame(1, count($logger->getLogs()));
}

public function testGetLogsWithoutDebugHandler()
public function testGetLogsWithoutDebugProcessor()
{
$handler = new TestHandler();
$logger = new Logger(__METHOD__, array($handler));
Expand All @@ -35,6 +39,9 @@ public function testGetLogsWithoutDebugHandler()
$this->assertSame(array(), $logger->getLogs());
}

/**
* @group legacy
*/
public function testCountErrorsWithDebugHandler()
{
$handler = new DebugHandler();
Expand All @@ -53,7 +60,10 @@ public function testCountErrorsWithDebugHandler()
$this->assertSame(4, $logger->countErrors());
}

public function testGetLogs()
/**
* @group legacy
*/
public function testGetLogsWithDebugHandler2()
{
$logger = new Logger('test');
$logger->pushHandler(new DebugHandler());
Expand All @@ -66,12 +76,55 @@ public function testGetLogs()
$this->assertEquals(Logger::INFO, $record['priority']);
}

public function testCountErrorsWithoutDebugHandler()
public function testCountErrorsWithoutDebugProcessor()
{
$handler = new TestHandler();
$logger = new Logger(__METHOD__, array($handler));

$this->assertTrue($logger->error('error message'));
$this->assertSame(0, $logger->countErrors());
}

public function testGetLogsWithDebugProcessor()
{
$handler = new TestHandler();
$processor = new DebugProcessor();
$logger = new Logger(__METHOD__, array($handler), array($processor));

$this->assertTrue($logger->error('error message'));
$this->assertSame(1, count($logger->getLogs()));
}

public function testCountErrorsWithDebugProcessor()
{
$handler = new TestHandler();
$processor = new DebugProcessor();
$logger = new Logger(__METHOD__, array($handler), array($processor));

$this->assertTrue($logger->debug('test message'));
$this->assertTrue($logger->info('test message'));
$this->assertTrue($logger->notice('test message'));
$this->assertTrue($logger->warning('test message'));

$this->assertTrue($logger->error('test message'));
$this->assertTrue($logger->critical('test message'));
$this->assertTrue($logger->alert('test message'));
$this->assertTrue($logger->emergency('test message'));

$this->assertSame(4, $logger->countErrors());
}

public function testGetLogsWithDebugProcessor2()
{
$handler = new TestHandler();
$logger = new Logger('test', array($handler));
$logger->pushProcessor(new DebugProcessor());

$logger->addInfo('test');
$this->assertCount(1, $logger->getLogs());
list($record) = $logger->getLogs();

$this->assertEquals('test', $record['message']);
$this->assertEquals(Logger::INFO, $record['priority']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;

class AddDebugLogProcessorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('profiler')) {
return;
}
if (!$container->hasDefinition('monolog.logger_prototype')) {
return;
}
if (!$container->hasDefinition('debug.log_processor')) {
return;
}

$definition = $container->getDefinition('monolog.logger_prototype');
$definition->addMethodCall('pushProcessor', array(new Reference('debug.log_processor')));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;

use Doctrine\Common\Annotations\Reader;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -468,6 +469,12 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con

$definition->replaceArgument(4, $debug);
$definition->replaceArgument(6, $debug);

if ($debug && class_exists(DebugProcessor::class)) {
$definition = new Definition(DebugProcessor::class);
$definition->setPublic(false);
$container->setDefinition('debug.log_processor', $definition);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle;

use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
Expand Down Expand Up @@ -95,6 +96,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1);
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
0