首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Artisan命令的自定义错误和异常处理程序

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

Stack Overflow用户
提问于 2014-04-14 18:15:23
回答 1查看 3.5K关注 0票数 2

我试图通过Laravel4artisan命令向raygun.io发送错误和异常,但是Laravel似乎有自己的异常处理程序。

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

代码语言:javascript
复制
<?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正在用它自己的自定义实现来捕获它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-14 20:04:31

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

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

代码语言:javascript
复制
App::error(function(Exception $exception)
{
    Log::error($exception);
});

app/start/artisan中,您的手工只使用异常处理程序。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23067234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档