From 87bd741f2d4f6b66f03b5856d6bafa9fa7e7f6c5 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 4 Oct 2017 16:43:22 +0200 Subject: [PATCH] [Console][HttpKernel] Handle new SHELL_VERBOSITY, also configures the default logger --- src/Symfony/Component/Console/Application.php | 21 ++++++++++++++++++- src/Symfony/Component/Console/CHANGELOG.md | 1 + .../Console/Tests/ApplicationTest.php | 7 +++++++ .../DependencyInjection/LoggerPass.php | 12 ++++------- src/Symfony/Component/HttpKernel/Kernel.php | 6 ++++++ .../Component/HttpKernel/Log/Logger.php | 17 +++++++++++++-- .../DependencyInjection/LoggerPassTest.php | 12 ----------- 7 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index da9d803d1b..481a63e732 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -895,18 +895,37 @@ class Application } } + switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) { + case -1: $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); break; + case 1: $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); break; + case 2: $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); break; + case 3: $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); break; + default: $shellVerbosity = 0; break; + } + if (true === $input->hasParameterOption(array('--quiet', '-q'), true)) { $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); - $input->setInteractive(false); + $shellVerbosity = -1; } else { if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) { $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); + $shellVerbosity = 3; } elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) { $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); + $shellVerbosity = 2; } elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) { $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); + $shellVerbosity = 1; } } + + if (-1 === $shellVerbosity) { + $input->setInteractive(false); + } + + putenv('SHELL_VERBOSITY='.$shellVerbosity); + $_ENV['SHELL_VERBOSITY'] = $shellVerbosity; + $_SERVER['SHELL_VERBOSITY'] = $shellVerbosity; } /** diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 24d7ff7af4..946ff1e0ba 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 3.4.0 ----- + * added `SHELL_VERBOSITY` env var to control verbosity * added `CommandLoaderInterface`, `FactoryCommandLoader` and PSR-11 `ContainerCommandLoader` for commands lazy-loading * added a case-insensitive command name matching fallback diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index d96f5e98f8..396c35c733 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -1580,6 +1580,13 @@ class ApplicationTest extends TestCase $this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found'); } } + + protected function tearDown() + { + putenv('SHELL_VERBOSITY'); + unset($_ENV['SHELL_VERBOSITY']); + unset($_SERVER['SHELL_VERBOSITY']); + } } class CustomApplication extends Application diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php index 2cffb99b32..2ad7f32228 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php @@ -12,7 +12,6 @@ namespace Symfony\Component\HttpKernel\DependencyInjection; use Psr\Log\LoggerInterface; -use Psr\Log\LogLevel; use Symfony\Component\HttpKernel\Log\Logger; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -29,17 +28,14 @@ class LoggerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - $alias = $container->setAlias(LoggerInterface::class, 'logger'); - $alias->setPublic(false); + $container->setAlias(LoggerInterface::class, 'logger') + ->setPublic(false); if ($container->has('logger')) { return; } - $loggerDefinition = $container->register('logger', Logger::class); - $loggerDefinition->setPublic(false); - if ($container->getParameter('kernel.debug')) { - $loggerDefinition->addArgument(LogLevel::DEBUG); - } + $container->register('logger', Logger::class) + ->setPublic(false); } } diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 986b7cbcd6..6f4f2b72b6 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -177,6 +177,12 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { if (false === $this->booted) { + if ($this->debug && !isset($_SERVER['SHELL_VERBOSITY'])) { + putenv('SHELL_VERBOSITY=3'); + $_ENV['SHELL_VERBOSITY'] = 3; + $_SERVER['SHELL_VERBOSITY'] = 3; + } + $this->boot(); } diff --git a/src/Symfony/Component/HttpKernel/Log/Logger.php b/src/Symfony/Component/HttpKernel/Log/Logger.php index cf20ca258a..c43936ed40 100644 --- a/src/Symfony/Component/HttpKernel/Log/Logger.php +++ b/src/Symfony/Component/HttpKernel/Log/Logger.php @@ -37,15 +37,28 @@ class Logger extends AbstractLogger private $formatter; private $handle; - public function __construct($minLevel = LogLevel::WARNING, $output = 'php://stderr', callable $formatter = null) + public function __construct($minLevel = null, $output = 'php://stderr', callable $formatter = null) { + if (!$minLevel) { + $minLevel = LogLevel::WARNING; + + if (isset($_SERVER['SHELL_VERBOSITY'])) { + switch ((int) $_SERVER['SHELL_VERBOSITY']) { + case -1: $minLevel = LogLevel::ERROR; break; + case 1: $minLevel = LogLevel::NOTICE; break; + case 2: $minLevel = LogLevel::INFO; break; + case 3: $minLevel = LogLevel::DEBUG; break; + } + } + } + if (!isset(self::$levels[$minLevel])) { throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel)); } $this->minLevelIndex = self::$levels[$minLevel]; $this->formatter = $formatter ?: array($this, 'format'); - if (false === $this->handle = @fopen($output, 'a')) { + if (false === $this->handle = is_resource($output) ? $output : @fopen($output, 'a')) { throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output)); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LoggerPassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LoggerPassTest.php index d018929b2a..b199e11dab 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LoggerPassTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LoggerPassTest.php @@ -13,7 +13,6 @@ namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Psr\Log\LogLevel; use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass; use Symfony\Component\HttpKernel\Log\Logger; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -54,15 +53,4 @@ class LoggerPassTest extends TestCase $this->assertSame(Logger::class, $definition->getClass()); $this->assertFalse($definition->isPublic()); } - - public function testSetMinLevelWhenDebugging() - { - $container = new ContainerBuilder(); - $container->setParameter('kernel.debug', true); - - (new LoggerPass())->process($container); - - $definition = $container->getDefinition('logger'); - $this->assertSame(LogLevel::DEBUG, $definition->getArgument(0)); - } }