【发布时间】:2015-03-21 17:51:57
【问题描述】:
在 Symfony 中,当我们运行命令时,如果有任何通知或警告,Symfony 会显示一个大红色消息并退出整个程序。
我希望将错误发送到日志,但我不希望整个程序退出。谁能告诉我如何实现这一目标。谢谢。
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class NotQuitWithWarningOrNoticeCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('try:nored')
->setDescription('Avoid to show big red message')
->addArgument('type');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$type = $input->getArgument('type');
if ($type == 'warning' || !$type) {
trigger_error("a warning", E_USER_WARNING);
}
if ($type == 'notice' || !$type) {
trigger_error("a notice", E_USER_NOTICE);
}
echo "Hi there!\n";
}
}
(这里的代码只是为了重现问题。收到警告或通知是我的场景。)
【问题讨论】:
-
使用独白,不要触发错误。
-
但有时 php 函数和 3rd 方库会触发错误。
-
那很好。错误和警告是错误的代码/用法,您应该修复它们
标签: php symfony command warnings notice