Laravel 5删除堆栈跟踪

问题描述 投票:6回答:5

如何关闭或删除Laravel 5中的堆栈跟踪。如果您想在控制台中读取它们,则会很烦人。我知道你可以在app/Exceptions/Handler.php中添加自定义处理程序,但我不知道该怎么做。

php laravel laravel-5 stack-trace
5个回答
12
投票

APP_DEBUG=false文件中设置.env适用于前端。

如果您不希望仅在日志文件中输出堆栈跟踪行,请尝试此操作。

/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


6
投票

由于我无法评论或编辑@Harold答案,这里有一个改进的解决方案:

  1. 在/app/Exceptions/Handler.php中添加使用Log;在顶部,
  2. 然后修改报告功能:
    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);
        }
    }

0
投票

哈罗德和贾尼的答案是正确的。

但是,日志行不如默认行详细。

最好的解决方案是编辑文件:

供应商/ laravel /框架/ SRC /照亮/日志/ LogManager.php

并在调用includeStacktraces方法时添加false参数。

添加false到

$ formatter-> includeStacktraces();

所以:

$ formatter-> includeStacktraces(假);

这将简单地禁用堆栈跟踪。其他一切都是一样的。


0
投票

对于那些不喜欢“添加这个,删除那种”指令的人,这里是基于Laravel 5.7的app/Exceptions/Handler.php应该是什么样子,并且坚持像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日志以便快速检查。


-3
投票

只需在你的env文件中设置你的qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.