如何从Laravel的错误报告中关闭或删除堆栈跟踪?如果你想在控制台上阅读它们,它们会很烦人。我知道您可以在app/Exceptions/Handler.php
中添加自定义处理程序,但我不知道如何做到这一点。
在.env
文件中设置APP_DEBUG=false
对前端工作很好。
如果您不希望堆栈跟踪行仅在日志文件中输出,请尝试此操作。
在/app/Exceptions/Handler.php
中在顶部添加use Log;
,然后在报告功能中添加此:
Log::error('['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file']);
和删除:
parent::report($e);
更多信息:https://laracasts.com/discuss/channels/laravel/remove-stacktrace-from-log-files
由于我不能评论或编辑@Harold的回答,这里是一个改进的解决方案:
- 在/app/Exceptions/Handler.php中添加use Log;在顶部
- 然后修改报表功能:
对于那些不喜欢"添加这个,删除那个"指令的人,下面是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);
}
}
注意,您可以将Log::error()
替换为对error_log()
的简单调用,以写入PHP的标准错误日志。如果这样做,则不必删除对parent::report()
的调用。这样,你就可以在Laravel日志中保留这些跟踪信息,以便进一步调试,同时保留你的PHP主日志,以便快速检查。
另一种方法是在日志配置文件中编辑日志格式。这意味着你不必覆盖默认的report
函数,这将意味着错过父函数中的其他逻辑。
我把我的设置为:
'daily' => [
'driver' => 'daily',
'formatter' =>LineFormatter::class,
'formatter_with' => [
'format' => "[%datetime%] %channel%.%level_name%: %message%'n %context%'n",
'dateFormat' => "Y-m-d H:i:s"
],
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
只需在env文件中设置APP_DEBUG=false
。
Harold和Jani的答案是正确的。
但是日志行没有默认行那么详细
最好的解决方案是编辑文件:
供应商/laravel/框架/src/照明/日志/LogManager.php
并在调用includeStacktraces方法时添加false参数。
为
添加false格式化程序-> includeStacktraces ();
:
格式化程序-> includeStacktraces(假);
这将简单地禁用堆栈跟踪。