laravelvalidationlogginglaravel-6laravel-formrequest

Laravel Easiest way to log the Form Request Validation actual error messages


I am working in Laravel v6.18 project for client.

Now randomly when I try to submit form with quite an amount of data, I am getting the error as below:

[2020-09-17 10:05:23] local.ERROR: Illuminate\Validation\ValidationException: The given data was invalid. in [LaravelDir]\vendor\laravel\framework\src\Illuminate\Foundation\Http\FormRequest.php:130
Stack trace:
....

I know this is a generic error message created to hide the actual error from the end user, but I want the actual error to be logged.

If I want to log this actual error without directly editing the Vendor package, I have to override, but I have no clue which method of which class to override for logging actual Form Request validation error(which Form field validation failed).

Can anyone assist in this ?


Solution

  • I would suggest doing it in Handler file in report method you will check for ValidationException then log the validation errors like so

    if ($exception instanceof ValidationException) {
        Log::error("errors", $exception->errors());
    }