问题
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.
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
}
tried
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: 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);
}
This way we use Laravel routing without having to rewrite the old code immediately.
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.
ideas
- Use some wildcard in
$dontReport
. - Use a
@
suppression at the right place - Can it be http://php.net/manual/en/scream.examples-simple.php
edit to explain after which steps middleware didn't work
1) create midddleware
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/app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SuppressExceptions::class,
],
回答1:
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);
}
回答2:
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.
回答3:
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.
回答4:
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;
回答5:
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).
回答6:
how i do is in
app/providers/AppServiceProvider
in boot function
public function boot()
{
//add error reporting level
error_reporting(0);
}
回答7:
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
来源:https://stackoverflow.com/questions/44806474/disable-error-reporting-entirely-in-laravel-production