如何在 Laravel 10 中记录验证异常

问题描述 投票:0回答:2

我正在尝试追踪向我的服务器发出请求的外部 API 的一个非常具体的问题。如何更改异常处理程序以便将请求验证异常记录在错误日志文件中?预先感谢您的帮助。

php laravel exception handler laravel-10
2个回答
0
投票

自定义例外情况

php artisan make:exception

文档 Laravel 错误

Log::error('验证错误:' .$exception->getMessage());


0
投票

前往您的

app/Exceptions/Handler.php

编辑

register
方法:

use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
/**
 * Register the exception handling callbacks for the application.
 *
 * @return void
 */
public function register()
{
    $this->reportable(function (Throwable $e) {
        if ($e instanceof ValidationException) {
            Log::error($e->getMessage(), $this->buildExceptionContext($e));
        }
    });
}

现在,每当发生验证错误时,都会将其写入您的日志,而不会更改向最终用户报告的方式。

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