Skip to content

[Console][HttpKernel] Handle new SHELL_VERBOSITY env var, also configures the default logger #24425

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
Oct 10, 2017
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
21 changes: 20 additions & 1 deletion src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,18 +895,37 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
}
}

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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,13 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public function shutdown()
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();
}

Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/HttpKernel/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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;
Expand Down Expand Up @@ -54,15 +53,4 @@ public function testRegisterLogger()
$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));
}
}