Introduction to Errors & Logging in Laravel 5.4

Share:
Errors & Logging in Laravel 5.4

Introduction

In software/ web development Error logging plays a vital role in the maintenance and debugging process of application or project. It is believed that at the time of development if you add error logging/ tracing capabilities in your project then in future if any bugs or errors will occur or produced then it will reduce the time of fixing those bugs. Logging helps you to quickly traces bugs and evaluate the health of the application in development or maintenance period.
Laravel comes with excellent error and exception handling feature. When you install Laravel framework (MVC) error and exception handling is configured at same time.
Laravel App\Exceptions\Handler class is responsible for handling all exceptions triggered by your application and logs them or display them to app users.

For logging error, Laravel imitates Monolog (the error logging library).

Monolog is an error Logging library for PHP that provides support for a variety of powerful log handlers. Monolog allow you to sends logs to files on disk, sockets, inboxes/mails, databases and various web services.Many of these log handlers are automatically configured by Laravel. Laravel allow you to choose a single or rotating log files or writing error information directly to the system log.


Configuring Error Detail in Laravel: How much error information is displayed or showed to End Users

Laravel provide an easy way for configuring Errors, the debug option of config/app.php (Laravel's configuration file) handles the what information about an error is actually displayed to the users. By default, debug option is set with respect to the value of the APP_DEBUG environment variable, which is stored in your .env file (.env file is available in root directory laravel's project).

For local (development), you should set the APP_DEBUG environment variable to true, so if any errors are occured or genertaed at development time then you can get detailed information about that error. In the production environment, this value must always be false, otherwise, you have a risk of exposing sensitive configuration values/information to your application's end users that will be a security threat.


Configuring Log Storage in Laravel: Managing how, where and at what frequency logs are saved

Laravel supports multiple options to write log information. You can write your log information to single files, daily files, the Syslog, and the error logs.In order to configure Laravel's storage mechanism,modify log option of config/app (Laravel's configuration file).By default it set to single file, if you wish to change this from single to daily log files, you should set the log value in your app configuration file i.e config/app.php to daily by changing "single" to "daily"
'log' => 'daily'

Configuring Log Severity Levels in Laravel

In Laravel ,you can manage Log Severity Levels by adding log_level option in config/app.php in Laravel's configuration file. Once you have configured log_level option , Laravel will start logging all levels greater than or equal to the specified severity of log. For example, a default log_level of error will log error or critical or alert or emergency messages
'log_level' => env('APP_LOG_LEVEL', 'error'),


No comments