Symfony provides a Console component for creating command line applications. When building console commands, it's essential to consider the verbosity level, which determines the amount of information displayed during command execution. Symfony allows developers to control the verbosity level, tailoring the output to meet different needs, from basic information to detailed debugging. This tutorial explains how to control verbosity level for console command in Symfony 8.
Symfony provides several verbosity levels that can be adjusted to control the amount of information displayed on the console.
Verbosity level can be controlled by --silent, -q and -v options, or by setting the SHELL_VERBOSITY environment variable to control verbosity level globally for all commands. Note that the --silent, -q and -v options take precedence over the value of SHELL_VERBOSITY.
| Console option | SHELL_VERBOSITY value | PHP constant |
|---|---|---|
--silent | -2 | - |
-q or --quiet | -1 | OutputInterface::VERBOSITY_QUIET |
| (none) | 0 | OutputInterface::VERBOSITY_NORMAL |
-v | 1 | OutputInterface::VERBOSITY_VERBOSE |
-vv | 2 | OutputInterface::VERBOSITY_VERY_VERBOSE |
-vvv | 3 | OutputInterface::VERBOSITY_DEBUG |
We can display a message in a command based on a particular verbosity level. For instance:
src/Command/TestCommand.php
<?php
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'app:test')]
class TestCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Normal verbosity');
$output->writeln('Normal verbosity', OutputInterface::VERBOSITY_NORMAL);
$output->writeln('Verbose -v', OutputInterface::VERBOSITY_VERBOSE);
$output->writeln('Very verbose -vv', OutputInterface::VERBOSITY_VERY_VERBOSE);
$output->writeln('Debug -vvv', OutputInterface::VERBOSITY_DEBUG);
return self::SUCCESS;
}
}
We can control verbosity level as follows:
| Command | Output |
|---|---|
php bin/console app:test -q | (No output) |
php bin/console app:test | Normal verbosity Normal verbosity |
php bin/console app:test -v | Normal verbosity Normal verbosity Verbose -v |
php bin/console app:test -vv | Normal verbosity Normal verbosity Verbose -v Very verbose -vv |
php bin/console app:test -vvv | Normal verbosity Normal verbosity Verbose -v Very verbose -vv Debug -vvv |
While -q suppresses normal command output, exceptions are still displayed. Symfony also provides the --silent option, which suppresses all output, including exceptions. To demonstrate this behavior, consider the following command that throws an exception:
src/Command/TestCommand.php
<?php
namespace App\Command;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'app:test')]
class TestCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
throw new RuntimeException('Not implemented');
}
}
Now compare the output when running the command with different verbosity options:
| Command | Output |
|---|---|
php bin/console app:test --silent | (No output) |
php bin/console app:test -q | In TestCommand.php line 16: Not implemented |
Leave a Comment
Cancel reply