skip to Main Content

I’m trying to track down a very specific issue with an external API making requests to my server. How can I make changes to my exception handler so that request validation exceptions get logged in my error log file? Thanks in advance for any help.

3

Answers


  1. Chosen as BEST ANSWER

    Thanks for all the answers, but I was able to find the answer here: https://stackoverflow.com/a/73066132/9980193

    This was the method I added to Handler.php:

    use IlluminateValidationValidationException;
    
    
     public function convertValidationExceptionToResponse(ValidationException $e, $request)
        {
    
            Log::info([
                'errors' => $e->errors(),
                'request' => $request->all(),
            ]);
    
            return parent::convertValidationExceptionToResponse($e, $request);
        }
    

  2. custom exceptions

    php artisan make:exception
    

    doc laravel errors

    Log::error(‘Validation Error: ‘ . $exception->getMessage());

    Login or Signup to reply.
  3. Go to your app/Exceptions/Handler.php

    Edit the register method:

    use IlluminateSupportFacadesLog;
    use IlluminateValidationValidationException;
    
    /**
     * 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));
            }
        });
    }
    

    Now whenever a validation error occurs, it will be written to your log without changing how it is reported to your end user.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search