Tutorialsplane

Laravel Errors & Logging


Laravel Errors & Logging – The Laravel Errors & Logging both are different things. Errors and exception handling is already configured for you when you started new project. Logging is important mechanism which can log errors that are generated.


Laravel Errors & Logging.

Let us understand how to use laravel Errors & Logging.

Function:-

There are some followings function available in laravel Errors & Logging.

1. Introduction.

Errors.

When we install a new project. It will be already configured for you. We need to see errors for debugging purpose in a local enviroment. We need to hide these errors from users. The variable APP_DEBUG are achieved to set in environment file.

Logging.

It is an important mechanism that are generated log errors by system. Its support different logging mode like single, daily, syslog and errorlog mode. You can set mode in config/app.php file.

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

'log' => 'daily'

2. Configuration.

Error Detail

The debug option detemine how much information about an error is actually display to the users. The debug option is placed in config/app.php. By default, the value of APP_DEBUG id stored in .env file.

In local environment the APP_DEBUG value should be true but in production we need to be set to false to hide errors.

Log Storage

You want to modify then you should go in log option in your config/app.php configuration file.

Maximum Daily Log Files

If you are using daily log mode. By default Laravel will only return only five days of log files. If you want to adjust the retained file you may add a log_max_files configure value in your app configure file.

'log_max_files' => 30

Log Severity Levels

When using monolog, the message has different level of severity. Laravel writes all log level to storage by default. In production environment, you may wish to configure minimum severity should be logged by adding the log_level option to your app.php configuration file.

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

Custom Monolog Configuration

If you want to take complete controll over how monolog is configured for your application. You can use the application ConfigMonologUsing.

Let’s look at a simple example.

$app->configureMonologUsing(function ($monolog) {
    $monolog->pushHandler(...);
});

return $app;

3. The Exception Handler.

The exception handler contain two method Repot and Render.

The Report Method

It is used to log exception and send them to Bugsnag and Sentry. This method simply pass the exception to the base class where the exception is logged.

Let’s look at a simple example.

public function report(Exception $exception)
{
    if ($exception instanceof CustomException) {
        //
    }

    return parent::report($exception);
}

Ignoring Exceptions By Type

The $dontReport property of exception handler contains an array of exception types that will not be logged.

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

This method is responsible for converting a given exception into an HTTP response that should be sent back to the browser.

Let’s look at a simple example.

public function render($request, Exception $exception)
{
    if ($exception instanceof CustomException) {
        return response()->view('errors.custom', [], 500);
    }

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

4. HTTP Exceptions..

Exception describe HTTP error code from the server.

abort(404);

The abort helper will immediately raise an exception which will be rendered by the exception handler.

abort(403, 'Unauthorized action.');

Custom HTTP Error Pages

If you want to customize an error page for 404 HTTP status code, create resources/views/errors/404.blade.php.

<h2>{{ $exception->getMessage() }}</h2>

5. Logging.

Laravel provides a simple abstraction layer on top of the powerful Monolog library.

Let's look at a simple example.

<?php namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function showProfile($id)
    {
        Log::info('Showing user profile for user: '.$id);

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

The logger defined eight level in RFC 5424.

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

Contextual Information

Contexual data can also be passed to the log method. It will be formatted and display with log message.

Log::info('User failed to login.', ['id' => $user->id]);

Accessing The Underlying Monolog Instance

Monolog has a variety of additional handlers you may use for logging.

$monolog = Log::getMonolog();

Laravel Tutorial