Disable error reporting entirely in Laravel production?

核能气质少年 提交于 2019-12-03 06:47:45

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).

Try changing the render method to

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);
}

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);
}

I guess your php.ini loaded from another place. So settings still not applied. Try to find correct location of php.ini (you can see information in the phpinfo()). Anyway, you can rewrite those parameters with yours in index.php:

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

But as the @Davon says in the comment. Those settings will be overwritten by Laravel. So code above can be placed in your controller. But it will be dirty hack. So You have to find another way. Try to print your .env's content. Maybe some setting is incorrect.

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

The second line changes the value of 'display_errors' in the php.ini file

EDIT: Add more code to show how this has to be environment specific...

$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;

Laravel debug settings is located in the .env file, in which you can set the debug option as follows:

APP_DEBUG = true

but...

Laravel also have config mechanism that located in app.php in the config folder, which defaults to:

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

which tells Laravel to use the .env value, and defaults to false, but anyone with access to the file, can simply change it to:

'debug' => true,

so that your .env value gets ignored by Laravel.

If the problem is that you are seeing the 'Whoops something went wrong' page, you could fix that with the answer @alepeino wrote:

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

But I'd change the render method to:

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

        return response('nothing', 500);
    }

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

This render method (the parent) is the one that builds and returns the html for the "Whoops" page, so if you overwrite it, you should be cool.

To change de debug config, check if your config/app.php has the debug options using the ENV value APP_DEBUG, and on your production .env, check it's set to false (APP_DEBUG=false).

how i do is in

app/providers/AppServiceProvider 

in boot function

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

Laravel emphasizes error and warning free code, so the best way to handle this is just by making sure your code doesn't produce any errors, warnings, or notices.

Update: I do not recommend the below method for recent versions of Laravel. Laravel now allows you to change exception handling in a non-vendor class: App\Exceptions\Handler as indicated by alepeino's answer. A middleware can also be a better solution to disabling error_reporting.

This previous answer is maintained for historical purposes but I don't recommend modifying vendor files.


If, however, you still decide to alter this behavior, you'll need to look in a file named HandleExceptions.php normally found at vendor/laravel/framework/src/illuminate/Foundation/Bootstrap/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');
    }
}

Line 32 where error_reporting is currently set to -1. https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Of course, by modifying this code, you'll either need to prevent updates of laravel/framework or you'll need to verify this file on every update.

After updating this code, you'll need to recompile your classes:

php artisan clear-compiled; php artisan optimize
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!