Artisan命令的自定义错误和异常处理程序


Custom error and exception handler for Artisan command

我正试图通过Laravel 4 artisan命令将错误和异常发送到raygun.io,但Laravel似乎有自己的异常处理程序。

有没有一种方法可以让我在命令中指定自定义方法?目前,我正在尝试为错误和异常指定一个自定义处理程序,如下所示:

<?php
class ParseCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'my:parse';
    protected $descriptino = '...';
    protected $raygun = null;
    /**
     * __construct
     */
    public function __construct()
    {
        parent::__construct();
        // Setup custom error and exception handlers
        $raygun_api_key = 'Config::get('tms.raygun_api_key');
        $this->raygun = new RaygunClient("MUzf+furi8E9tffcHAaJVw==");
        set_exception_handler([$this, 'exceptionHandler']);
        set_error_handler([$this, 'errorHandler']);
    }
    /**
     * Custom exception handler.
     *
     * @param $exception
     */
    public function exceptionHandler($exception)
    {
        $this->raygun->SendException($exception);
    }
    /**
     * Custom error handler.
     *
     * @param $errno
     * @param $errstr
     * @param $errfile
     * @param $errline
     */
    public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        $this->raygun->SendError($errno, $errstr, $errfile, $errline);
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        $start_time = microtime(true);
        throw new 'Exception("Oopsie!");
        // Omitted for brevity...
    }
}

当然,处理程序永远不会执行,因为Artisan正在用自己的自定义实现来获取它们。

文件夹app/start中的文件仅在相应环境运行时启动Laravel框架时执行。这意味着当环境等于local时运行app/start/local.php,而当环境等于staging时运行app/start/staging.php。此外,app/start/global.php文件每次都会运行,app/start/artisan.php在每次手工执行时都会运行。

根据文档:http://laravel.com/docs/errors放置处理程序

App::error(function(Exception $exception)
{
    Log::error($exception);
});

app/start/artisan中为您的手工异常处理程序。