I have an application built in laravel 5.8 which is in production mode. As the site is live I don't want to display any errors to our users. But If some error occurs then I want the log number or any reference that indicates what error was thrown at that particular time.
So I wanted to know if there is any way to display "Oops something went wrong ref#123456" to our users. So that they can send us a screenshot or reference number and we can track what actually happend by checking our log file.
Thanks in advance. Happy coding.
By default, Laravel is configured to create a single log file for your application, and this file is stored in app/storage/logs/laravel. log .
You can see the generated log entries in storage/logs/laravel. log file.
As quick start, you can set the error_reporting and display_errors directives in your computer's system-wide php. ini file (details here). However, Laravel should have its own error reporting features—make sure you check them out in their documentation. you may need to increase your memory limit .
In Laravel 5 you can catch exceptions by editing the render method in app/Exceptions/Handler. php . This will be applied to ANY exception in AJAX requests. If your app is sending out an exception of App\Exceptions\MyOwnException , you check for that instance instead.
I thing that you can do it saving the logs in database and render its id.
You can do it in App\Exceptions\Handler
:
https://laravel.com/docs/5.8/errors
For example:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
// Create log in db
$log = Log::create([
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'line' => $exception->getLine(),
]);
// Print log id in logs
logger("LogId {$log->id}");
//Return view error with log id
return return response()->view('errors', ['logId' => $log->id]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With