【问题标题】:symfony 2.1 + try / catch block isn't working in a console commandsymfony 2.1 + try / catch 块在控制台命令中不起作用
【发布时间】:2014-10-11 20:25:34
【问题描述】:

我正在使用通过 cron 执行的控制台命令。我想捕获每个异常并将其写入日志,但我的捕获根本不起作用。

引发错误的部分代码是在调用“ejecutar”时(这是故意的),该错误会在控制台中打印,但 catch 之后的代码不会在日志中打印任何内容

<?php
namespace daci\contratosBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

//logger
use Psr\Log\LoggerInterface;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Templating\EngineInterface;

class SeguimientosRemainderEmailCommand extends ContainerAwareCommand
{
    //Administrador
    protected $emailAdmin       = "....@.....sv";
    protected $clasePhp         = "SeguimientosRemainderEmailCommand.php";
    protected $nombreProceso    = "SeguimientosRemainderEmail";
    protected $comando          = "dacicontratosBundle:seguimientos_remainder_email";
    protected $descripcion      = "Recordatorio semanal para seguimiento de contratos";
    protected $tiempoEjecucion  = "Lun | 8:00am";
    protected $mensaje          = "";
    protected $asunto           = "ITIGES - Servicio de Notificaciones - ";

    protected $logger;
    protected $text         = "";
    protected function configure()
    {
        $this->setName($this->comando)
             ->setDescription($this->descripcion)
             ->addArgument('my_argument', InputArgument::OPTIONAL, 'Argument description');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {


        try{
            //logger
            $this->logger = $this->getContainer()->get('logger');
            $this->text = strtoupper('---- INICIO SEGUIMIENTOS REMAINDER EMAIL -----');
            $this->logger->info($this->text);

            $this->getContainer()->enterScope('request');
            $this->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request');

            ejecutar(); //HERE IS WHERE IT FAILS


            $this->mensaje = "<strong>Éxito</strong>" ."<br/>";
            $this->mensaje = $this->mensaje . "<strong>Clase php:</strong>              ".$this->clasePhp."<br/>";
            $this->mensaje = $this->mensaje . "<strong>Comando ejecutado:</strong>      ".$this->comando."<br/>";
            $this->mensaje = $this->mensaje . "<strong>Descripción:</strong>            ".$this->descripcion."<br/>";
            $this->mensaje = $this->mensaje . "<strong>Ejecución programada:</strong>   ".$this->tiempoEjecucion."<br/>";
            $datetime = new \DateTime();
            $this->mensaje = $this->mensaje . "<strong>Hora de ejecución:</strong>      ".$datetime->format('Y\-m\-d\ h:i:s');

            $this->enviarMailAdmin($this->comando, $this->mensaje);


        }catch (\Exception $re) {
            $this->logger->error($re->getMessage());
        }
    }

这是我试图捕捉的错误,但我想捕捉任何类型的错误/异常

Fatal error: Call to undefined function daci\contratosBundle\Command\ejecutar()
in C:\Dropbox\Apache Xampp\evaluacion_daci\src\daci\contratosBundle\Command\Segu
imientosRemainderEmailCommand.php on line 82

知道发生了什么吗?

【问题讨论】:

  • 您确定日志记录已打开吗?您是否在跟踪日志文件并看到日志文件中有任何回显?
  • 嗨!日志运行良好
  • 如果您 var_dump 或 echo 异常消息,它会打印在您的输出中吗?
  • 不!唯一向我展示的是控制台命令中的错误。
  • @RodolVelasco 你找到原因了吗?我遇到了同样的问题

标签: php symfony try-catch console-application symfony-2.1


【解决方案1】:

从 PHP 的基础知识: try{} catch(){} 块可以捕获仅作为 Exception 抛出的错误。

您没有在此处粘贴错误日志(请执行此操作),但我认为这是一个致命错误,或类似的错误。

请记住,这些错误是无法捕获的。因此,即使在 try-catch 中,它们也会破坏脚本。

【讨论】:

  • 我想捕捉各种错误并将其写入日志。我编辑了这个问题,以便您可以看到我想要了解的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多