完全在Laravel生产中禁用错误​​报告? [英] Disable error reporting entirely in Laravel production?

查看:150
本文介绍了完全在Laravel生产中禁用错误​​报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完全在生产环境中禁用错误​​报告,因为我们有一些非常旧的代码仍需要修复,但目前可以正常工作(是的,我也不喜欢).我们无法在几天内解决所有问题,因此我们需要像往常一样抑制警告和异常.

I would like to disable error reporting entirely on production, because we have some very old code we still need to fix but for now does work (yes I don't like it either). We cannot fix everything in a few days, so we need to just supress the warnings and exceptions like we always did.

真正的问题是,它已经在一个简单的惰性错误(例如因为未定义var)上引发了异常

The real problem is that it already throws an exception on a simple lazy bug like (because var is not defined)

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

尝试

APP_DEBUG = false

APP_DEBUG=false

APP_LOG_LEVEL =紧急

APP_LOG_LEVEL=emergency

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

但是它仍然显示ErrorException

未定义变量:script_name_vars_def

Undefined variable: script_name_vars_def

编辑:代码的工作方式如下

edit: The code works like this

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路由而不必立即重写旧代码.

This way we use Laravel routing without having to rewrite the old code immediately.

我知道我可以很轻松地解决此警告,但是这些错误有很多很多,我们现在需要使用Laravel路由.以后再解决问题.

I know I can fix this warning very easy, but there are many, many more of these errors and we need to use Laravel routing now. Fix the problems later.

想法

  • Use some wildcard in $dontReport.
  • Use a @ suppression at the right place
  • Can it be http://php.net/manual/en/scream.examples-simple.php

编辑以解释中间件哪些步骤不起作用

1)创建中间件

php artisan make:middleware SuppressExceptions

2)写下

SuppressExceptions.php

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

3)注册

laravel/app/Http/Kernel.php

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

推荐答案

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

Yes you can change the error reporting. In fact, the framework provides a place to intercept the exceptions: App\Exceptions\Handler. By default the render method will convert the exception thrown to a HTML response. The APP_ENV and APP_DEBUG values will only change how this error response will render (details on the exception stack trace or not, basically).

尝试将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可能会满足您的需求,但是请注意,您可能无法通过这种方式从致命错误中恢复.

This basically turns reporting off and then attempts to re-handle the request. In the if clause you may check for any condition you want (the class of the exception, the severity, etc.). Catching ErrorException will probably cover your needs, but notice that you may not be able to recover from a fatal error this way.

无论如何,您应该将其视为概念证明" ...对于非幂等请求,这种重新处理"方法不好.相反,只需使用

Anyway, you should take that as a "proof of concept"... For non-idempotent requests, this "re-handle" approach is not good. Instead, just create a Middleware with

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

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

Same as before, fatal errors can't be recovered this way. But you can show a custom error message combining this middleware with the exception handler approach from before:

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

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

这篇关于完全在Laravel生产中禁用错误​​报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆