php 在 Laravel 生产中完全禁用错误报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44806474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable error reporting entirely in Laravel production?
提问by online Thomas
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)
真正的问题是它已经在一个简单的懒惰错误上抛出异常(因为没有定义 var )
if(!$var) {
// do whatever
}
tried
试过了
APP_DEBUG=false
APP_LOG_LEVEL=emergency
APP_DEBUG=假
APP_LOG_LEVEL=紧急
display_errors(false);
set_error_handler(null);
set_exception_handler(null);
But it still shows an ErrorException
但它仍然显示一个 ErrorException
Undefined variable: script_name_vars_def
未定义变量:script_name_vars_def
edit: The code works like this
编辑:代码是这样工作的
web.php
网页.php
Route::any('/someroute', 'somecontroller@controllerFunc');
somecontroller.php
一些控制器.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.
这样我们就可以使用 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.
我知道我可以很容易地修复这个警告,但是还有很多很多这样的错误,我们现在需要使用 Laravel 路由。稍后修复问题。
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
- 在
$dontReport
. @
在正确的地方使用压制- 可以是http://php.net/manual/en/scream.examples-simple.php
edit to explain after which steps middleware didn't work
编辑以解释中间件在哪些步骤后不起作用
1) create midddleware
1)创建中间件
php artisan make:middleware SuppressExceptions
2) Write it
2)写出来
SuppressExceptions.php
抑制异常.php
public function handle($request, Closure $next)
{
error_reporting(0);
return $next($request);
}
3) Register
3) 注册
laravel/app/Http/Kernel.php
laravel/app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SuppressExceptions::class,
],
回答by alepeino
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).
是的,您可以更改错误报告。事实上,框架提供了一个拦截异常的地方:App\Exceptions\Handler
. 默认情况下,该render
方法会将抛出的异常转换为 HTML 响应。该APP_ENV
和APP_DEBUG
值仅改变这种错误的反应如何渲染(在异常堆栈跟踪信息与否,基本上)。
Try changing the render
method to
尝试将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);
}
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.
这基本上会关闭报告,然后尝试重新处理请求。在if
子句中,您可以检查您想要的任何条件(异常的类别、严重性等)。捕获ErrorException
可能会满足您的需求,但请注意,您可能无法通过这种方式从致命错误中恢复。
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 Middlewarewith
无论如何,您应该将其视为“概念证明”......对于非幂等请求,这种“重新处理”方法并不好。相反,只是创建一个中间件与
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);
}
回答by am05mhz
Laravel debug settings is located in the .env
file, in which you can set the debug option as follows:
Laravel 调试设置位于.env
文件中,您可以在其中设置调试选项,如下所示:
APP_DEBUG = true
but...
但...
Laravel also have config mechanism that located in app.php
in the config
folder, which defaults to:
Laravel 也有位于文件夹app.php
中的配置机制,config
默认为:
'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:
它告诉 Laravel 使用该.env
值,并默认为false
,但任何有权访问该文件的人都可以简单地将其更改为:
'debug' => true,
so that your .env
value gets ignored by Laravel.
这样你的.env
价值就会被 Laravel 忽略。
回答by danny
how i do is in
我是怎么做的
app/providers/AppServiceProvider
in boot function
在启动功能
public function boot()
{
//add error reporting level
error_reporting(0);
}
回答by Alex Slipknot
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
:
我猜你是php.ini
从另一个地方加载的。所以设置仍然没有应用。尝试找到正确的位置php.ini
(您可以在 中查看信息phpinfo()
)。无论如何,你可以用你的参数重写这些参数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.
但正如@Davon 在评论中所说。这些设置将被 Laravel 覆盖。所以上面的代码可以放在你的控制器中。但这将是肮脏的黑客。所以你必须找到另一种方式。尝试打印您.env
的内容。也许某些设置不正确。
回答by Pila
error_reporting(0);
ini_set('display_errors', 0);
The second line changes the value of 'display_errors' in the php.ini
file
第二行更改php.ini
文件中'display_errors'的值
EDIT: Add more code to show how this has to be environment specific...
编辑:添加更多代码以显示这必须是特定于环境的...
$env = getenv('APPLICATION_ENV');
$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;
回答by Juan Pablo
If the problem is that you are seeing the 'Whoops something went wrong' page, you could fix that with the answer @alepeino wrote:
如果问题是您看到“哎呀出了问题”页面,您可以通过@alepeino 写道的答案来解决这个问题:
https://stackoverflow.com/a/44862789/2777970
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.
这个 render 方法(父)是为“Whoops”页面构建和返回 html 的方法,所以如果你覆盖它,你应该很酷。
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).
要更改调试配置,请检查您的 config/app.php 是否具有使用 ENV 值 APP_DEBUG 的调试选项,并在您的生产 .env 上检查它是否设置为 false (APP_DEBUG=false)。
回答by Devon
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.
Laravel 强调无错误和无警告代码,因此处理这个问题的最好方法就是确保您的代码不会产生任何错误、警告或通知。
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.
更新:我不建议对 Laravel 的最新版本使用以下方法。Laravel 现在允许您更改非供应商类中的异常处理:App\Exceptions\Handler,如 alepeino 的回答所示。中间件也可以是禁用 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:
但是,如果您仍然决定更改此行为,则需要查看通常位于 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');
}
}
Line 32 where error_reporting is currently set to -1. https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
第 32 行,其中 error_reporting 当前设置为 -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.
当然,通过修改这段代码,你要么需要阻止 laravel/framework 的更新,要么你需要在每次更新时验证这个文件。
After updating this code, you'll need to recompile your classes:
更新此代码后,您需要重新编译您的类:
php artisan clear-compiled; php artisan optimize