AppDividend
Latest Web Development Tutorials

Laravel 5.5 Logs and Errors

Laravel Logs

425

Laravel 5.5 Logs and Errors is today’s main topic. When you start a new project then Laravel Logs are important for you to debug the application. In case of Laravel, it is configured out of the box. An App\Exceptions\Handler class is where all the exceptions are triggered and logged in the storage file and then return back to the user. Laravel used monolog library to utilize the logs generated by our Laravel application.

It allows us to configure the log files using following options as per its official documentation.

  1. Single log file.
  2. Rotating log files.
  3. Writing error information to the system log.

Laravel 5.5 Logs and Errors

Laravel 5.5 provides us one option in a .env file called APP_DEBUG which needs to be true in order to get the detail of any error messages.  If you are in a development environment then it must be true and in production it is false.

Log Storage

By default, Laravel 5.5 writes the log in a single file.

Log Severity Levels

When we are using monolog library, there are following severity levels.

  1. debug
  2.  info
  3.  notice
  4.  warning,
  5. error
  6.  critical
  7.  alert
  8.  emergency

 We may configure the minimum severity that should be logged by adding the optionlog_level to your app.phpconfiguration file.

'log_level' => env('APP_LOG_LEVEL', 'error'),

Exception Handler

All the exceptions are handled by an App\Exceptions\Handler class. This class contains two methods.

  1. The Report Method
  2. The Render Method
Related Posts
1 of 8

Report Method

By default, the reportmethod simply passes the exception to the base class where the exception is logged.

They pass the exceptions to the base class services like bugsnag or sentry

Ignoring Exceptions By Type

App\Exceptions\Handler.php file will look like this.

// Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}

If we wish the exceptions that would not be logged then we need to specify $dontReport property array.

// Handler.php

/**
 * A list of the exception types that should not be reported.
 *
 * @var array
 */
protected $dontReport = [
    \Illuminate\Auth\AuthenticationException::class,
    \Illuminate\Auth\Access\AuthorizationException::class,
    \Symfony\Component\HttpKernel\Exception\HttpException::class,
    \Illuminate\Database\Eloquent\ModelNotFoundException::class,
    \Illuminate\Validation\ValidationException::class,
];

The Render Method

A Render method is responsible for converting the given exception into the HTTP response sent back to the browser.



// Handler.php

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if ($exception instanceof CustomException) {
        return response()->view('errors.custom', [], 500);
    }

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

For example, following URL, I am trying to access…

http://localhost:8000/item/23

If item number 23 is not found then Laravel by default send us an exception and in response in Laravel 5.5 Page not found view is a return to the browser.

Logging

You may write information to the logs using the Log facade:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        Log::info('Showing user profile for user: '.$id);

        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

The logger provides the eight logging levels: emergencyalertcriticalerrorwarningnoticeinfo and debug.

Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);

So, finally our tutorial on Laravel 5.5 Logs and Errors tutorial is over.

Leave A Reply

Your email address will not be published.