Verbosity Levels

Verbosity Levels

Console commands have different verbosity levels, which determine the messages displayed in their output. By default, commands display only the most useful messages, but you can control their verbosity with the -q and -v options:

  1. # do not output any message (not even the command result messages)
  2. $ php bin/console some-command -q
  3. $ php bin/console some-command --quiet
  4. # normal behavior, no option required (display only the useful messages)
  5. $ php bin/console some-command
  6. # increase verbosity of messages
  7. $ php bin/console some-command -v
  8. # display also the informative non essential messages
  9. $ php bin/console some-command -vv
  10. # display all messages (useful to debug errors)
  11. $ php bin/console some-command -vvv

The verbosity level can also be controlled globally for all commands with the SHELL_VERBOSITY environment variable (the -q and -v options still have more precedence over the value of SHELL_VERBOSITY):

Console optionSHELL_VERBOSITY valueEquivalent PHP constant
-q or —quiet-1OutputInterface::VERBOSITY_QUIET
(none)0OutputInterface::VERBOSITY_NORMAL
-v1OutputInterface::VERBOSITY_VERBOSE
-vv2OutputInterface::VERBOSITY_VERY_VERBOSE
-vvv3OutputInterface::VERBOSITY_DEBUG

It is possible to print a message in a command for only a specific verbosity level. For example:

  1. // ...
  2. use Symfony\Component\Console\Command\Command;
  3. use Symfony\Component\Console\Input\InputInterface;
  4. use Symfony\Component\Console\Output\OutputInterface;
  5. class CreateUserCommand extends Command
  6. {
  7. // ...
  8. public function execute(InputInterface $input, OutputInterface $output): int
  9. {
  10. $user = new User(...);
  11. $output->writeln([
  12. 'Username: '.$input->getArgument('username'),
  13. 'Password: '.$input->getArgument('password'),
  14. ]);
  15. // available methods: ->isQuiet(), ->isVerbose(), ->isVeryVerbose(), ->isDebug()
  16. if ($output->isVerbose()) {
  17. $output->writeln('User class: '.get_class($user));
  18. }
  19. // alternatively you can pass the verbosity level PHP constant to writeln()
  20. $output->writeln(
  21. 'Will only be printed in verbose mode or higher',
  22. OutputInterface::VERBOSITY_VERBOSE
  23. );
  24. return 0;
  25. }
  26. }

When the quiet level is used, all output is suppressed as the default [write()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Output/Output.php "Symfony\Component\Console\Output\Output::write()") method returns without actually printing.

Tip

The MonologBridge provides a Symfony\Bridge\Monolog\Handler\ConsoleHandler class that allows you to display messages on the console. This is cleaner than wrapping your output calls in conditions. For an example use in the Symfony Framework, see How to Configure Monolog to Display Console Messages.

Tip

The full exception stacktrace is printed if the VERBOSITY_VERBOSE level or above is used.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.