Cory Tutton
Cory Tutton

Reputation: 59

Laravel: logging errors, and showing a nice view

I'm trying to log errors in Laravel but it throws the Chrome default 500 error page before even reaching the render function, how am I supposed to catch any errors?

I want to log all errors to the database and show a nice user friendly CUSTOM view but it how can I when it doesn't make it to the render method?

Laravel doesn't make it to:

public function render($request, Exception $exception)

So how are you meant to log errors? This seems wrong in so many ways.

public function render($request, Exception $exception)
{
    if (strlen($exception->getMessage()) > 0) {
        $agent = new Agent();
        $errorLog = new ErrorLog;
        $errorLog->error_message = $exception->getMessage();
        $errorLog->error_file = $exception->getFile();
        $errorLog->error_line = $exception->getLine();
        $errorLog->request_ip = $request->ip();
        $errorLog->request_url = $request->root();
        $errorLog->request_device = $agent->isDesktop() ? 'Desktop' : ($agent->isMobile() ? 'Mobile' : 'Tablet');
        $errorLog->request_system = $agent->platform() . ' ' . $agent->version($agent->platform());
        $errorLog->request_browser = $agent->browser();
        $errorLog->error_happened_to = (Auth::check() ? Auth::user()->username : 'Guest');
        $errorLog->save();
    }

    return parent::render($request, $exception);
}

Also, my .EVN file:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=*************************************************
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=************

Upvotes: 1

Views: 4763

Answers (3)

Samuel James
Samuel James

Reputation: 1536

Laravel has a report method you can use to log or do anything you want when errors occur

//File App\Exceptions\Handler.php; In Handler.php add or modify this method.

public function report(Exception $exception)
{
    if ($this->shouldReport($exception)) { //prevent uneccessary error reporting such as 404 errors

       $e =FlattenException::create($exception);
        $handler =new \Symfony\Component\Debug\ExceptionHandler();
        $error_message =$handler->getHtml($e); ///Note this will give u full details of the error
            $agent = new Agent();
    $errorLog = new ErrorLog;
    $errorLog->error_message = $error_message;
    $errorLog->error_file = $exception->getFile();
    $errorLog->error_line = $exception->getLine();
    $errorLog->request_ip = $request->ip();
    $errorLog->request_url = $request->root();
    $errorLog->request_device = $agent->isDesktop() ? 'Desktop' : ($agent->isMobile() ? 'Mobile' : 'Tablet');
    $errorLog->request_system = $agent->platform() . ' ' . $agent->version($agent->platform());
    $errorLog->request_browser = $agent->browser();
    $errorLog->error_happened_to = (Auth::check() ? Auth::user()->username : 'Guest'

    parent::report($exception);
}

To see your nice view, modify .env and change APP_DEBUG from true to false e.g APP_DEBUG=false

You can also decide to log errors only when app is on production.

if (App::environment('production')) {
 //log stuffs
}

Your custom 500 error in

resources/views/errors/500.blade.php

enter image description here Laravel will automatically serve that page

Upvotes: 0

This is how I handle the same issue. In app/Exceptions/Handler.php:

public function report(Exception $exception)
{
    if(!config('app.debug')) {
        if ($this->shouldReport($exception)) {
            $this->logError($exception);
        }
    }
    if(env('APP_ENV') == 'local'){
        parent::report($exception);
    }
}

public function render($request, Exception $exception)
{
    if(!config('app.debug')) {
        if($this->shouldReport($exception)){
            return response()->view('errors.500', compact('exception'));
        }
    }
    return parent::render($request, $exception);
}

I added a function logError that writes the error to the database, and I have a template in resources/views/errors - 500.blade.php - that has a custom error page.

I also use the APP_DEBUG in the .env to determine whether to log the error to the database and display the error page or to show the error details on screen.

Upvotes: 4

Hiren Makwana
Hiren Makwana

Reputation: 2156

You can try third party packages for log & show errors.

Eg.

  1. Laravel 5 log viewer
  2. LogViewer

Upvotes: 3

Related Questions