跳到内容

详细程度级别

编辑此页

控制台命令具有不同的详细程度级别,这些级别决定了在其输出中显示的消息。默认情况下,命令仅显示最有用的消息,但你可以使用 -q-v 选项来控制其详细程度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# suppress all output, including errors
$ php bin/console some-command --silent

# suppress all output (even the command result messages) but display errors
$ php bin/console some-command -q
$ php bin/console some-command --quiet

# normal behavior, no option required (display only the useful messages)
$ php bin/console some-command

# increase verbosity of messages
$ php bin/console some-command -v

# display also the informative non essential messages
$ php bin/console some-command -vv

# display all messages (useful to debug errors)
$ php bin/console some-command -vvv

7.2

--silent 选项在 Symfony 7.2 中引入。

详细程度级别也可以通过 SHELL_VERBOSITY 环境变量全局控制所有命令(-q-v 选项仍然比 SHELL_VERBOSITY 的值具有更高的优先级)

控制台选项 SHELL_VERBOSITY 等效的 PHP 常量
--silent -2 OutputInterface::VERBOSITY_SILENT
-q--quiet -1 OutputInterface::VERBOSITY_QUIET
(无) 0 OutputInterface::VERBOSITY_NORMAL
-v 1 OutputInterface::VERBOSITY_VERBOSE
-vv 2 OutputInterface::VERBOSITY_VERY_VERBOSE
-vvv 3 OutputInterface::VERBOSITY_DEBUG

可以在命令中仅针对特定的详细程度级别打印消息。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// ...
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CreateUserCommand extends Command
{
    // ...

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $user = new User(...);

        $output->writeln([
            'Username: '.$input->getArgument('username'),
            'Password: '.$input->getArgument('password'),
        ]);

        // available methods: ->isSilent(), ->isQuiet(), ->isVerbose(), ->isVeryVerbose(), ->isDebug()
        if ($output->isVerbose()) {
            $output->writeln('User class: '.get_class($user));
        }

        // alternatively you can pass the verbosity level PHP constant to writeln()
        $output->writeln(
            'Will only be printed in verbose mode or higher',
            OutputInterface::VERBOSITY_VERBOSE
        );

        return Command::SUCCESS;
    }
}

7.2

isSilent() 方法在 Symfony 7.2 中引入。

当使用静默或安静级别时,所有输出都将被抑制,因为默认的 write() 方法返回时实际上不进行打印。

提示

当使用 silent 详细程度时,错误不会显示在控制台中,但它们仍将通过 Symfony 日志记录器集成进行记录。

提示

MonologBridge 提供了一个 ConsoleHandler 类,允许你在控制台上显示消息。这比将输出调用包装在条件中更简洁。有关 Symfony 框架中的示例用法,请参阅 如何配置 Monolog 以显示控制台消息

提示

如果使用 VERBOSITY_VERBOSE 级别或更高级别,则会打印完整的异常堆栈跟踪。

本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本