Add url (and other info) to your laravel Exception context

Laravel 11: bootstrap/app.php

Add the full url, environment and the used HTTP method to the context of your exception so it gets logged with all needed information:

<?php
  // file: bootstrap/app.php
  
return Application::configure(basePath: dirname(__DIR__))
    ...
    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->context(fn () => [
            'app' => config('app.name'),
            'url' => url()->full(),
            'environment' => app()->environment(),
            'method' => request()->getMethod(),
        ]);
    })->create();

Laravel 10: app/Exceptions/Handler.php

props to @_newtonjob this tweet

<?php
// In laravel 10
// file app/Exceptions/Handler.php
class Handler extends ExceptionHandler
{
   
    public function register(): void
    {
       
       //
    }

    public function context()
    {
        return array_merge(parent::context(), [
            'app' => config('app.name'),
            'url' => url()->full(),
            'environment' => app()->environment(),
            'method' => request()->getMethod(),
        ]);
    }
}

Click Here to Leave a Comment Below

Leave a Reply: