Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel capture whoops app-level exception and post to log with unique ID#

I am looking to make error reporting from an app more friendly to users. I am going to replace the 'Whoops' screen (in production) with a form allowing the user to submit the problem. I am wondering if there is a simple way to add a specific ID# (unique integer) to the stack trace in the error log so that we can easily view specific errors which were generated on production.

like image 325
phirschybar Avatar asked Jan 21 '26 05:01

phirschybar


1 Answers

The simple way to do this is in app/start/global.php. Under the Application Error Handler you want to log your error, and generate a GUID (or something similar).

App::error(function(Exception $exception, $code)
{
    // Generate a unique ID for this error...
    $unique_id = uniqid();

    // log the error
    Log::error(str_repeat('-', 40));
    Log::error("Exception for $unique_id");
    Log::error($exception);

    // return error form
    return View::make('whoops_error_form')->with('unique_id', $unique_id);

});

Your whoops_error_form template would have a hidden form somewhere where you will be able to submit the application error. All reports would be logged in app/storage/logs.

By returning the view on App::error, you will disable the other exception handlers (such as Whoops!)

like image 90
Tim Groeneveld Avatar answered Jan 23 '26 21:01

Tim Groeneveld