Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying the error in production mode with log number in laravel

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.

like image 426
Faran Khan Avatar asked May 14 '20 17:05

Faran Khan


People also ask

Where is the Laravel error log?

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 .

How do I view Laravel logs?

You can see the generated log entries in storage/logs/laravel. log file.

How do I enable error mode in Laravel?

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 .

How do I get exception error in Laravel?

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.


1 Answers

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]);
    }

like image 103
Luis Ozuna Avatar answered Oct 17 '22 06:10

Luis Ozuna