在Laravel生产中完全禁用错误报告?

时间:2017-06-28 15:13:19

标签: php laravel laravel-5 laravel-5.3 laravel-5.4

我想完全禁止生产错误报告,因为我们有一些非常旧的代码,我们仍然需要修复,但现在确实有效(是的,我也不喜欢它)。我们无法在几天内修复所有内容,因此我们需要像往常一样压制警告和异常。

真正的问题是它已经在一个简单的懒惰错误上引发异常(因为没有定义var)

if(!$var) {
     // do whatever
}

试图

  

APP_DEBUG =假

     

APP_LOG_LEVEL =紧急

display_errors(false);
set_error_handler(null);
set_exception_handler(null);

但它仍会显示ErrorException

  

未定义的变量:script_name_vars_def

编辑:代码就像这样

web.php

Route::any('/someroute', 'somecontroller@controllerFunc');

somecontroller.php

public controllerFunc() {
    ob_start();
    require '/old_index.php';
    $html = ob_get_clean();

    return response($html);
}

这样我们就可以使用Laravel路由,而无需立即重写旧代码。

我知道我可以很容易地修复此警告,但是这些错误中有很多甚至更多,我们现在需要使用Laravel路由。稍后解决问题。

想法

编辑解释中间件无法运行的步骤

1)创建midddleware

php artisan make:middleware SuppressExceptions

2)写下来

SuppressExceptions.php

public function handle($request, Closure $next)
{
    error_reporting(0);
    return $next($request);
}

3)注册

laravel /应用/ HTTP / Kernel.php

protected $middlewareGroups = [
   'web' => [
       \App\Http\Middleware\SuppressExceptions::class,
],

8 个答案:

答案 0 :(得分:7)

是的,您可以更改错误报告。事实上,该框架提供了一个拦截异常的地方:App\Exceptions\Handler。默认情况下,render方法会将抛出的异常转换为HTML响应。 APP_ENVAPP_DEBUG值只会更改此错误响应的呈现方式(基本上是否有异常堆栈跟踪的详细信息)。

尝试将render方法更改为

public function render($request, Exception $exception)
{
    if ($exception instanceof ErrorException) {
        error_reporting(0);

        $kernel = app(\Illuminate\Contracts\Http\Kernel::class);
        $response = $kernel->handle($request)->send();
        return $kernel->terminate($request, $response);
    }

    return parent::render($request, $exception);
}

这基本上会关闭报告,然后尝试重新处理请求。 在if子句中,您可以检查所需的任何条件(异常的类,严重性等)。捕获ErrorException可能会满足您的需求,但请注意您可能无法通过这种方式从致命错误中恢复。

无论如何,你应该把它当作“概念证明”......对于非幂等请求,这种“重新处理”的方法并不好。相反,只需create a Middleware

public function handle($request, Closure $next)
{
    error_reporting(0);
    return $next($request);
}

与以前一样,致命错误无法通过这种方式恢复。但是,您可以显示一个自定义错误消息,将此中间件与之前的异常处理程序方法相结合:

public function render($request, Exception $exception)
{
    if ($exception instanceof FatalErrorException) {
        return view('fatal-error', ['exception' => $exception]);
    }

    return parent::render($request, $exception);
}

答案 1 :(得分:3)

我猜您的php.ini已从其他地方加载。因此设置仍未应用。尝试找到php.ini的正确位置(您可以在phpinfo()中查看信息)。无论如何,您可以在index.php

中重写这些参数
error_reporting(0);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);

但正如@Davon在评论中所说。这些设置将被Laravel覆盖。因此,上面的代码可以放在您的控制器中。但这将是肮脏的黑客。所以你必须找到另一种方式。尝试打印.env的内容。也许某些设置不正确。

答案 2 :(得分:2)

error_reporting(0);
ini_set('display_errors', 0);

第二行更改了php.ini文件

中'display_errors'的值

编辑:添加更多代码以显示这必须是特定于环境的......

$ env = getenv('APPLICATION_ENV');

 switch ($env) {
        case 'production':
            error_reporting(0);
            $config = include __DIR__ . '/../app/config/config_prod.php';
            break;

        case 'staging':
            ini_set('display_errors', 1);
            $config = include __DIR__ . '/../app/config/config_staging.php';
            break;

        case 'development':
        case 'local':
        default:
            ini_set('display_errors', 1);
            $config = include __DIR__ . '/../app/config/config_local.php';
            break;

答案 3 :(得分:2)

Laravel调试设置位于.env文件中,您可以在其中设置调试选项,如下所示:

APP_DEBUG = true

但是...

Laravel还有app.php文件夹中位于config的配置机制,默认为:

'debug' => env('APP_DEBUG', false),

告诉Laravel使用.env值,默认为false,但有权访问该文件的任何人都可以将其更改为:

'debug' => true,

以便Laravel忽略您的.env值。

答案 4 :(得分:2)

如果问题是你看到了'哎呀'出了问题'页面,你可以用@alepeino写的答案解决这个问题:

https://stackoverflow.com/a/44862789/2777970

但是我将渲染方法更改为:

public function render($request, Exception $exception)
{
    if (!config('app.debug')) {
        error_reporting(0);

        return response('nothing', 500);
    }

    return parent::render($request, $exception);
}

这个渲染方法(父)是构建并返回" Whoops"的html的方法。页面,所以如果你覆盖它,你应该很酷。

要更改de debug config,请检查config / app.php是否具有使用ENV值APP_DEBUG的调试选项,并在生产.env上检查它是否设置为false(APP_DEBUG = false)。 / p>

答案 5 :(得分:1)

Laravel强调错误和警告免费代码,因此处理此问题的最佳方法是确保您的代码不会产生任何错误,警告或通知。

更新:我不推荐以下方法用于最新版本的Laravel。 Laravel现在允许您更改非供应商类中的异常处理:App \ Exceptions \ Handler,如alepeino的回答所示。中间件也可以是禁用error_reporting的更好解决方案。

以前的答案是出于历史目的而维护的,但我不建议修改供应商文件。

但是,如果您仍然决定改变此行为,则需要查看通常位于vendor / laravel / framework / src / illuminate / Foundation / Bootstrap / HandleExceptions.php中的名为HandleExceptions.php的文件。 :

public function bootstrap(Application $app)
{
    $this->app = $app;
    error_reporting(-1); // change this line to your desired reporting level
    set_error_handler([$this, 'handleError']);
    set_exception_handler([$this, 'handleException']);
    register_shutdown_function([$this, 'handleShutdown']);
    if (! $app->environment('testing')) {
        ini_set('display_errors', 'Off');
    }
}

第32行,其中error_reporting当前设置为-1。 https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

当然,通过修改此代码,您需要阻止更新laravel / framework,或者您需要在每次更新时验证此文件。

更新此代码后,您需要重新编译您的类:

php artisan clear-compiled; php artisan optimize

答案 6 :(得分:0)

我的工作方式是

app/providers/AppServiceProvider 

启动功能

public function boot()
{
   //add error reporting level
   error_reporting(0);
}

答案 7 :(得分:0)

在您的类的任何函数中使用此方法作为示例:

try {
    // ...
} catch (\Exception $e) {
    return redirect()->intended(route('home'));
}