Метод setVerbosity
Метод setVerbosity класса Output устанавливает уровень детализации вывода консольной команды. Первым параметром передаётся целое число, задающее уровень: OutputInterface::VERBOSITY_QUIET, OutputInterface::VERBOSITY_NORMAL, OutputInterface::VERBOSITY_VERBOSE, OutputInterface::VERBOSITY_VERY_VERBOSE или OutputInterface::VERBOSITY_DEBUG. Чем выше уровень, тем больше сообщений выводится.
Синтаксис
$output->setVerbosity($level)
Пример
Давайте создадим команду, которая устанавливает уровень детализации в VERBOSITY_VERBOSE и выводит сообщение:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class VerbosityCommand extends Command
{
protected static $defaultName = 'app:verbosity';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
$output->writeln('Verbose message');
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
Verbose message
Пример
Давайте сравним поведение при разных уровнях детализации:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class VerbosityCompareCommand extends Command
{
protected static $defaultName = 'app:verbosity-compare';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
$output->writeln('Quiet message');
$output->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
$output->writeln('Normal message');
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
$output->writeln('Verbose message');
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
$output->writeln('Very verbose message');
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
$output->writeln('Debug message');
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
Normal message
Verbose message
Very verbose message
Debug message
Сообщение с уровнем QUIET не выводится, так как оно подавлено.