Laravel 5 删除堆栈跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30583567/
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
Laravel 5 remove stack trace
提问by Angelo A
How can you turn off or remove the stack traces from Laravel 5. They're annoying if you want to read them in your console. I know you can add a custom handler in app/Exceptions/Handler.php
, but I have no idea how to do that.
如何从 Laravel 5 中关闭或删除堆栈跟踪。如果您想在控制台中阅读它们,它们会很烦人。我知道您可以在 中添加自定义处理程序app/Exceptions/Handler.php
,但我不知道该怎么做。
回答by Harold
Setting APP_DEBUG=false
in your .env
file works fine for the frontend.
APP_DEBUG=false
在您的.env
文件中设置适用于前端。
If you don't want the stack trace lines to be outputted in the log files only, try this.
如果您不希望堆栈跟踪行仅在日志文件中输出,请尝试此操作。
In /app/Exceptions/Handler.php
add use Log;
at the top, then add this in the report function:
在/app/Exceptions/Handler.php
加use Log;
在上面,然后在报表功能补充一点:
Log::error('['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file']);
And remove:
并删除:
parent::report($e);
More info: https://laracasts.com/discuss/channels/laravel/remove-stacktrace-from-log-files
更多信息:https: //laracasts.com/discuss/channels/laravel/remove-stacktrace-from-log-files
回答by Jani
Since I can not comment on or edit @Harold answer, here is a improved sollution:
由于我无法评论或编辑@Harold 的答案,这里有一个改进的解决方案:
- In /app/Exceptions/Handler.php add use Log; at the top,
- Then modify the report function:
- 在/app/Exceptions/Handler.php 中添加使用日志;在顶部,
- 然后修改报表功能:
public function report(Exception $e) { if (!config('app.debug')) { Log::error('['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file']); } else { parent::report($e); } }
回答by miken32
For those who don't like "add this, remove that" kind of instructions, here's what app/Exceptions/Handler.php
should look like, based on Laravel 5.7, and sticking to a message format like PHP's native one:
对于那些不喜欢“添加这个,删除那个”这样的指令的人,这里是app/Exceptions/Handler.php
基于 Laravel 5.7 的应该是什么样子,并坚持像 PHP 的原生格式一样的消息格式:
<?php
namespace App\Exceptions;
use Log;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
Log::error(sprintf(
"Uncaught exception '%s' with message '%s' in %s:%d",
get_class($exception),
$exception->getMessage(),
$exception->getTrace()[0]['file'],
$exception->getTrace()[0]['line']
));
// parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
}
Note that you can replace Log::error()
with a simple call to error_log()
to write to PHP's standard error log. If you do that, you don't have to remove the call to parent::report()
. That way you can keep the traces in the Laravel log for further debugging, but keep your main PHP log for quick checks.
请注意,您可以替换Log::error()
为简单的调用error_log()
来写入 PHP 的标准错误日志。如果这样做,则不必删除对 的调用parent::report()
。这样,您可以将跟踪记录保留在 Laravel 日志中以供进一步调试,但保留您的主要 PHP 日志以进行快速检查。
回答by Tim
simplely set your APP_DEBUG=false
in your env file.
简单地APP_DEBUG=false
在你的 env 文件中设置你的。
回答by Claudio
Harold and Jani's answers are correct.
Harold 和 Jani 的回答是正确的。
However the log line is less detailed than the default one.
但是,日志行不如默认行详细。
The best solution would be to edit the file:
最好的解决方案是编辑文件:
vendor/laravel/frameworks/src/Illuminate/Log/LogManager.php
供应商/laravel/frameworks/src/Illuminate/Log/LogManager.php
and add falseargument when the includeStacktraces method is called.
并在调用 includeStacktraces 方法时添加false参数。
add false to
添加假到
$formatter->includeStacktraces();
$formatter->includeStacktraces();
so:
所以:
$formatter->includeStacktraces(false);
$formatter->includeStacktraces(false);
This will simply disable the stacktrace. Everything else remains the same.
这将简单地禁用堆栈跟踪。其他一切都保持不变。