Просмотр исходного кода

disable mail logging on cronjob execution with preview option

Olivier Massot 1 год назад
Родитель
Сommit
eb692e55eb
1 измененных файлов с 17 добавлено и 0 удалено
  1. 17 0
      src/Commands/CronCommand.php

+ 17 - 0
src/Commands/CronCommand.php

@@ -6,6 +6,7 @@ use App\Service\Cron\CronjobInterface;
 use App\Service\Cron\UI\ConsoleUI;
 use App\Service\Cron\UI\ConsoleUI;
 use App\Service\ServiceIterator\CronjobIterator;
 use App\Service\ServiceIterator\CronjobIterator;
 use Monolog\Formatter\LineFormatter;
 use Monolog\Formatter\LineFormatter;
+use Monolog\Handler\FingersCrossedHandler;
 use Monolog\Handler\RotatingFileHandler;
 use Monolog\Handler\RotatingFileHandler;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\Console\Attribute\AsCommand;
 use Symfony\Component\Console\Attribute\AsCommand;
@@ -114,6 +115,10 @@ class CronCommand extends Command
         $preview = $input->getOption('preview');
         $preview = $input->getOption('preview');
         $jobs = [];
         $jobs = [];
 
 
+        if ($preview) {
+            $this->disableLoggerEmailHandler();
+        }
+
         if (!array_key_exists($action, self::ACTIONS)) {
         if (!array_key_exists($action, self::ACTIONS)) {
             $this->output->writeln($formatter->formatBlock('Error: unrecognized action', 'error'));
             $this->output->writeln($formatter->formatBlock('Error: unrecognized action', 'error'));
 
 
@@ -251,4 +256,16 @@ class CronCommand extends Command
     {
     {
         $this->configureLoggerFormatter();
         $this->configureLoggerFormatter();
     }
     }
+
+    protected function disableLoggerEmailHandler()
+    {
+        $handlers = [];
+        /* @noinspection PhpPossiblePolymorphicInvocationInspection @phpstan-ignore-next-line */
+        foreach ($this->logger->getHandlers() as $handler) {
+            if (!$handler instanceof FingersCrossedHandler) {
+                $handlers[] = $handler;
+            }
+        }
+        $this->logger->setHandlers($handlers);
+    }
 }
 }