Laravel - Handling Errors

Laravel - Handling Errors

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

Important Points

Before proceeding 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 sent back to the user for analysis.

  • When your Laravel application is put into debug mode, verbose error messages with stack traces will be displayed for every error that occurs in your web application.

For any new project, Laravel logs errors and exceptions in the App\Exceptions\Handler class by default . They are then sent back to the user for analysis.

When your Laravel application is put into debug mode, verbose error messages with stack traces will be displayed for every error that occurs in your web application.

  • By default the debug mode is set to false and you can change it to true . This allows the user to trace all errors with stack traces.

By default the debug mode is set to false and you can change it to true . This allows the user to trace all errors with stack traces.

  • The Laravel project configuration includes a debug option that determines how much error information should be displayed to the user. By default, the web application has this setting set to the value defined in the environment variables of the .env file .

    • The value is true in the local development environment and false in the production environment.

    • If the value is true in a production environment, the risk of transferring sensitive information to end users is higher.

The Laravel project configuration includes a debug option that determines how much error information should be displayed to the user. By default, the web application has this setting set to the value defined in the environment variables of the .env file .

The value is true in the local development environment and false in the production environment.

If the value is true in a production environment, the risk of transferring sensitive information to end users is higher.

Error log

Logging bugs in a web application helps you track them and plan your strategy for fixing them. The log information can be configured in the web application in the config/app.php file . Pay attention to the following points while dealing with error log in Laravel −

  • Laravel uses the PHP monologue library.

  • The logging options used for error tracking are single, daily, syslog, and errorlog .

  • For example, if you want to log error messages to log files, you should set the log value in your application configuration to daily as shown in the below command −

Laravel uses the PHP monologue library.

The logging options used for error tracking are single, daily, syslog, and errorlog .

For example, if you want to log error messages to log files, you should set the log value in your application configuration to daily as shown in the below command −

'log' => env('APP_LOG','daily'),
  • If daily log mode is selected as the option , Laravel accepts error log for 5 days by default . If you want to change the maximum number of log files, you need to set the log_max_files parameter in the configuration file to the desired value.

If daily log mode is selected as the option , Laravel accepts error log for 5 days by default . If you want to change the maximum number of log files, you need to set the log_max_files parameter in the configuration file to the desired value.

'log_max_files' => 25;

Severity levels

Because Laravel uses the PHP Monolog logging library, various parameters are used to parse severity levels. Various severity levels are available: Error, Critical, Emergency, and Emergency messages . You can set the severity level as shown in the command below −