Skip to main content

Laravel – Error Handling

Laravel – Error Handling

Most web applications have specific mechanisms for error handling. Using these, they track errors and exceptions, and log them to analyze the performance. In this chapter, you will read about error handling in Laravel applications.

Important Points

Before proceeding further to learn in detail about error handling in Laravel, please note the following important points −
  • For any new project, Laravel logs errors and exceptions in the App\Exceptions\Handler class, by default. They are then submitted back to the user for analysis.
  • When your Laravel application is set in debug mode, detailed error messages with stack traces will be shown on every error that occurs within your web application.
  • By default, debug mode is set to false and you can change it to true. This enables the user to track all errors with stack traces.
    The configuration of Laravel project includes the debug option which determines how much information about an error is to be displayed to the user. By default in a web application, the option is set to the value defined in the environment variables of the .env file.
    • The value is set to true in a local development environment and is set to false in a production environment.
    • If the value is set to true in a production environment, the risk of sharing sensitive information with the end users is higher.
  • Error Log

    Logging the errors in a web application helps to track them and in planning a strategy for removing them. The log information can be configured in the web application in config/app.php file. Please note the following points while dealing with Error Log in Laravel −
    • Laravel uses monolog PHP logging library.
    • The logging parameters used for error tracking are single, daily, syslog and errorlog.
    • For example, if you wish to log the error messages in log files, you should set the log value in your app configuration to daily as shown in the command below −

    • If the daily log mode is taken as the parameter, Laravel takes error log for a period of 5 days, by default. If you wish to change the maximum number of log files, you have to set the parameter of log_max_files in the configuration file to a desired value.

      Severity Levels

      As Laravel uses monolog PHP logging library, there are various parameters used for analyzing severity levels. Various severity levels that are available are error, critical, alert and emergency messages. You can set the severity level as shown in the command below −

Popular posts from this blog

Laravel – Blade Templates

Laravel – Blade Templates Laravel 5.1 introduces the concept of using  Blade , a templating engine to design a unique layout. The layout thus designed can be used by other views, and includes a consistent design and structure. When compared to other templating engines, Blade is unique in the following ways − It does not restrict the developer from using plain PHP code in views. The blade views thus designed, are compiled and cached until they are modified. The complete directory structure of Laravel is shown in the screenshot given here. You can observe that all views are stored in the  resources/views  directory and the default view for Laravel framework is  welcome.blade.php . Please note that other blade templates are also created similarly. Steps for Creating a Blade Template Layout You will have to use the following steps to create a blade template layout − Step 1 Create a layout folder inside the  resources/views  folder. We are...

What is Laravel Framework?

What is Laravel Framework? Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern.

What is routing and how, and what are the different ways to write it?

What is routing and how, and what are the different ways to write it? All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group. For most applications, you will begin by defining routes in your routes/web.php file.