8000 feature #30339 [Monolog] Disable DebugLogger in CLI (lyrixx) · symfony/symfony@cbb0b81 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbb0b81

Browse files
committed
feature #30339 [Monolog] Disable DebugLogger in CLI (lyrixx)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Monolog] Disable DebugLogger in CLI | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | | Fixed tickets | #30333 symfony/monolog-bundle#165 #25876 | License | MIT | Doc PR | <details> <summary>Considering this code:</summary> ```php namespace App\Command; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class LeakCommand extends Command { protected static $defaultName = 'leak'; private $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output) { $reportedAt = time(); while (true) { $this->logger->info('Hello'); if (time() - $reportedAt >= 1) { $output->writeln(sprintf('%dMb', memory_get_usage() / 1024 / 1024)); $reportedAt = time(); } } } } ``` </details> Without the patch ``` >…/dev/labs/symfony/website-skeleton(monolog %) bin/console leak 7Mb 28Mb 51Mb 76Mb 97Mb ```` With the patch ``` >…/dev/labs/symfony/website-skeleton(monolog %) bin/console leak 6Mb 6Mb 6Mb 6Mb ``` Commits ------- 17533da [Monolog] Disable DebugLogger in CLI
2 parents 31be5cf + 17533da commit cbb0b81

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Bridge/Monolog/Logger.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ public function reset()
8080
}
8181
}
8282

83+
public function removeDebugLogger()
84+
{
85+
foreach ($this->processors as $k => $processor) {
86+
if ($processor instanceof DebugLoggerInterface) {
87+
unset($this->processors[$k]);
88+
}
89+
}
90+
91+
foreach ($this->handlers as $k => $handler) {
92+
if ($handler instanceof DebugLoggerInterface) {
93+
unset($this->handlers[$k]);
94+
}
95+
}
96+
}
97+
8398
/**
8499
* Returns a DebugLoggerInterface instance if one is registered with this logger.
85100
*

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddDebugLogProcessorPass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function process(ContainerBuilder $container)
3030
}
3131

3232
$definition = $container->getDefinition('monolog.logger_prototype');
33+
$definition->setConfigurator([__CLASS__, 'configureLogger']);
3334
$definition->addMethodCall('pushProcessor', [new Reference('debug.log_processor')]);
3435
}
36+
37+
public static function configureLogger($logger)
38+
{
39+
if (method_exists($logger, 'removeDebugLogger') && \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
40+
$logger->removeDebugLogger();
41+
}
42+
}
3543
}

0 commit comments

Comments
 (0)
0