2014-04-14 110 views
2

我试图从Laravel 4 artisan命令向raygun.io发送错误和异常,但Laravel似乎有它自己的异常处理程序。Artisan命令的自定义错误和异常处理程序

有没有办法让我在我的命令中指定一个自定义方法?目前,我想如下指定错误和异常的自定义处理程序:

<?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... 
    } 
} 

关过程中的处理程序从不执行,因为工匠用它自己的自定义实现抓住他们。

+0

签入文件'app/start/global.php'。应该有一个Laravel使用的异常处理程序。它应该很容易修改。我在文档中找到了这个http://laravel.com/docs/errors#handling-errors,它应该会给你更多的信息。 – user3158900

+0

相关:我写了[这个答案](https://stackoverflow.com/q/46819944/5209322)这可能有助于更新版本的Laravel。 –

回答

2

文件夹app/start中的文件仅在运行相应环境时引导Laravel框架时执行。这意味着app/start/local.php在环境等于local时运行,app/start/staging.php在环境等于staging时运行。每次运行app/start/global.php文件,并在每个工匠执行时运行app/start/artisan.php

从文档:http://laravel.com/docs/errors地方处理

App::error(function(Exception $exception) 
{ 
    Log::error($exception); 
}); 
app/start/artisan你只工匠异常处理程序

+0

是Laravel上一个版本的代码片段吗?尽管如此,这是非常有帮助的,我来到http://laravel.com/docs/errors,甚至不知道那里。你介意编辑你的答案,以便我可以接受它吗? –

+0

你想改变什么?也许你自己提出一个编辑?别客气。 – hannesvdvreken

相关问题