Turn off error reporting completely in Laravel production?

I would like to completely turn off error reporting, because we have very old code that we still need to fix, but for now it works (yes, I don't like it either). We cannot fix everything in a few days, so we just need to suppress warnings and exceptions, as we always did.

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

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

tried to

APP_DEBUG = false

APP_LOG_LEVEL = emergency

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

But it still shows an ErrorException

Undefined variable: script_name_vars_def

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

Thus, we use Laravel routing without having to rewrite old code immediately.

I know that I can very easily fix this warning, but there are many such errors, and we need to use Laravel routing now. Correct the problems later.

the ideas

change to explain after what steps middleware doesn't work

1) create middleware

 php artisan make:middleware SuppressExceptions 

2) Write it

SuppressExceptions.php

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

3) Register

Laravel / application /Http/Kernel.php

 protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\SuppressExceptions::class, ], 
+12
php laravel laravel-5
source share
8 answers

Yes, you can change the error message. In fact, the platform provides a place to catch exceptions: App\Exceptions\Handler . By default, the render method converts the thrown exception into an HTML response. The values ​​of APP_ENV and APP_DEBUG just how this error response will be displayed (basically, information about tracing the exception stack).

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 disables the reports and then tries to reprocess the request. In the if clause, you can check for any condition (exception class, severity, etc.). ErrorException will probably cover your needs, but note that you may not be able to recover from a fatal error in this way.

In any case, you should accept this as a "proof of concept" ... For non-idempotent requests, this "reprocessing" approach is not suitable. Instead, just create Middleware with

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

As before, fatal errors cannot be fixed in this way. But you can show your own error message combining this middleware with an exception handler approach:

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

I think your php.ini loaded from another place. Therefore, the settings are still not applied. Try to find the correct php.ini location (you can see the information in phpinfo() ). Anyway, you can rewrite these parameters with your own in index.php :

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

But as @Davon says in the comment. These settings will be overwritten by Laravel. Thus, the code above can be placed in your controller. But it will be a dirty hack. So you need to find another way. Try printing your .env content. Perhaps some setting is incorrect.

+3
source share
 error_reporting(0); ini_set('display_errors', 0); 

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

EDIT: add more code to show how this should 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; 
+2
source share

The Laravel debugging options are in the .env file, where you can set the debugging option as follows:

 APP_DEBUG = true 

but...

Laravel also has a configuration mechanism located in app.php in the config folder, by default:

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

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

 'debug' => true, 

so that your .env value .env ignored by Laravel.

+2
source share

Laravel focuses on code without errors and warnings, so the best way to handle this is to simply make sure your code does not generate errors, warnings, or notifications.

Update: I do not recommend the method below for the latest versions of Laravel. Laravel now allows you to modify exception handling in a non-vendor class: App \ Exceptions \ Handler, as stated in alepeino's answer. Middleware may also be the best solution to disable error_reporting.

This previous answer is supported for historical purposes, but I do not recommend modifying vendor files.


However, if you decide to change this behavior, you need to look for a file called HandleExceptions.php, which is usually located at vendor / laravel / framework / src / lightingate / 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, changing this code, you need to either prohibit laravel / framework updates, or check this file with each update.

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

 php artisan clear-compiled; php artisan optimize 
+1
source share

If the problem is that you see the β€œSomeone did wrong” page, you can fix this with @alepeino's answer:

fooobar.com/questions/846884 / ...

But I would change the rendering 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 rendering method (parent) is the one that builds and returns the html for the Oops page, so if you overwrite it, you should be cool.

To change the debug configuration, check if your config / app.php has debug parameters using the ENV value APP_DEBUG, and in your production .env check the false flag (APP_DEBUG = false).

+1
source share

how am i in

 appServiceProvider public function boot() { error_reporting(0); } 
0
source share
0
source share

All Articles