Laravel 5删除堆栈跟踪

Ang*_*o A 6 php stack-trace laravel laravel-5

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

Har*_*old 14

设置APP_DEBUG=false在你的.env文件中针对前端工作正常.

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

/app/Exceptions/Handler.phpuse Log;在上面,然后在报表功能补充一点:

Log::error('['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file']);
Run Code Online (Sandbox Code Playgroud)

并删除:

parent::report($e);
Run Code Online (Sandbox Code Playgroud)

更多信息:https://laracasts.com/discuss/channels/laravel/remove-stacktrace-from-log-files


小智 7

由于我不能评论或编辑@Harold的答案,这里有一个改进的sollution:

  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);
        }
    }


mik*_*n32 5

对于那些不喜欢“添加这个,删除那个”指令的人,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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您可以Log::error()用简单的调用来替换以error_log()写入 PHP 的标准错误日志。如果您这样做,则不必删除对 的调用parent::report()。这样,您可以在 Laravel 日志中保留跟踪以进行进一步调试,同时保留主 PHP 日志以进行快速检查。


小智 -4

APP_DEBUG=false只需在 env 文件中设置即可。