Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6231acc

Browse files
committed
feature #24425 [Console][HttpKernel] Handle new SHELL_VERBOSITY env var, also configures the default logger (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [Console][HttpKernel] Handle new SHELL_VERBOSITY env var, also configures the default logger | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - On the CLI, the behavior of the new default logger is not nice: it's verbosity is controlled by `kernel.debug`, where I would expect to be able to control it by mean of verbosity. To achieve this, I propose to handle a new `SHELL_VERBOSITY` env var, and use it to control both commands' verbosity, and the logger's log level. Commits ------- 87bd741 [Console][HttpKernel] Handle new SHELL_VERBOSITY, also configures the default logger
2 parents 2e3d422 + 87bd741 commit 6231acc

File tree

7 files changed

+53
-23
lines changed

7 files changed

+53
-23
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,37 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
895895
}
896896
}
897897

898+
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
899+
case -1: $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); break;
900+
case 1: $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); break;
901+
case 2: $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); break;
902+
case 3: $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); break;
903+
default: $shellVerbosity = 0; break;
904+
}
905+
898906
if (true === $input->hasParameterOption(array('--quiet', '-q'), true)) {
899907
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
900-
$input->setInteractive(false);
908+
$shellVerbosity = -1;
901909
} else {
902910
if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) {
903911
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
912+
$shellVerbosity = 3;
904913
} elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) {
905914
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
915+
$shellVerbosity = 2;
906916
} elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) {
907917
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
918+
$shellVerbosity = 1;
908919
}
909920
}
921+
922+
if (-1 === $shellVerbosity) {
923+
$input->setInteractive(false);
924+
}
925+
926+
putenv('SHELL_VERBOSITY='.$shellVerbosity);
927+
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
928+
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;
910929
}
911930

912931
/**

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* added `SHELL_VERBOSITY` env var to control verbosity
78
* added `CommandLoaderInterface`, `FactoryCommandLoader` and PSR-11
89
`ContainerCommandLoader` for commands lazy-loading
910
* added a case-insensitive command name matching fallback

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,13 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn
15801580
$this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found');
15811581
}
15821582
}
1583+
1584+
protected function tearDown()
1585+
{
1586+
putenv('SHELL_VERBOSITY');
1587+
unset($_ENV['SHELL_VERBOSITY']);
1588+
unset($_SERVER['SHELL_VERBOSITY']);
1589+
}
15831590
}
15841591

15851592
class CustomApplication extends Application

src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Psr\Log\LogLevel;
1615
use Symfony\Component\HttpKernel\Log\Logger;
1716
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1817
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -29,17 +28,14 @@ class LoggerPass implements CompilerPassInterface
2928
*/
3029
public function process(ContainerBuilder $container)
3130
{
32-
$alias = $container->setAlias(LoggerInterface::class, 'logger');
33-
$alias->setPublic(false);
31+
$container->setAlias(LoggerInterface::class, 'logger')
32+
->setPublic(false);
3433

3534
if ($container->has('logger')) {
3635
return;
3736
}
3837

39-
$loggerDefinition = $container->register('logger', Logger::class);
40-
$loggerDefinition->setPublic(false);
41-
if ($container->getParameter('kernel.debug')) {
42-
$loggerDefinition->addArgument(LogLevel::DEBUG);
43-
}
38+
$container->register('logger', Logger::class)
39+
->setPublic(false);
4440
}
4541
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ public function shutdown()
177177
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
178178
{
179179
if (false === $this->booted) {
180+
if ($this->debug && !isset($_SERVER['SHELL_VERBOSITY'])) {
181+
putenv('SHELL_VERBOSITY=3');
182+
$_ENV['SHELL_VERBOSITY'] = 3;
183+
$_SERVER['SHELL_VERBOSITY'] = 3;
184+
}
185+
180186
$this->boot();
181187
}
182188

src/Symfony/Component/HttpKernel/Log/Logger.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,28 @@ class Logger extends AbstractLogger
3737
private $formatter;
3838
private $handle;
3939

40-
public function __construct($minLevel = LogLevel::WARNING, $output = 'php://stderr', callable $formatter = null)
40+
public function __construct($minLevel = null, $output = 'php://stderr', callable $formatter = null)
4141
{
42+
if (!$minLevel) {
43+
$minLevel = LogLevel::WARNING;
44+
45+
if (isset($_SERVER['SHELL_VERBOSITY'])) {
46+
switch ((int) $_SERVER['SHELL_VERBOSITY']) {
47+
case -1: $minLevel = LogLevel::ERROR; break;
48+
case 1: $minLevel = LogLevel::NOTICE; break;
49+
case 2: $minLevel = LogLevel::INFO; break;
50+
case 3: $minLevel = LogLevel::DEBUG; break;
51+
}
52+
}
53+
}
54+
4255
if (!isset(self::$levels[$minLevel])) {
4356
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel));
4457
}
4558

4659
$this->minLevelIndex = self::$levels[$minLevel];
4760
$this->formatter = $formatter ?: array($this, 'format');
48-
if (false === $this->handle = @fopen($output, 'a')) {
61+
if (false === $this->handle = is_resource($output) ? $output : @fopen($output, 'a')) {
4962
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
5063
}
5164
}

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LoggerPassTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
16-
use Psr\Log\LogLevel;
1716
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
1817
use Symfony\Component\HttpKernel\Log\Logger;
1918
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -54,15 +53,4 @@ public function testRegisterLogger()
5453
$this->assertSame(Logger::class, $definition->getClass());
5554
$this->assertFalse($definition->isPublic());
5655
}
57-
58-
public function testSetMinLevelWhenDebugging()
59-
{
60-
$container = new ContainerBuilder();
61-
$container->setParameter('kernel.debug', true);
62-
63-
(new LoggerPass())->process($container);
64-
65-
$definition = $container->getDefinition('logger');
66-
$this->assertSame(LogLevel::DEBUG, $definition->getArgument(0));
67-
}
6856
}

0 commit comments

Comments
 (0)