2014-04-14 113 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

相關問題