Laravel - 错误处理

WBOY
发布: 2024-08-27 10:50:32
原创
550 人浏览过

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 theApp\Exceptions\Handlerclass, 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.

Error Log
  • By default, debug mode is set tofalseand you can change it totrue. This enables the user to track all errors with stack traces.

App Debug
  • The configuration of Laravel project includes thedebugoption 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.envfile.

    • The value is set totruein a local development environment and is set tofalsein a production environment.

    • If the value is set totruein 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 inconfig/app.phpfile. 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 aresingle, daily, sysloganderrorlog.

  • For example, if you wish to log the error messages in log files, you should set the log value in your app configuration todailyas shown in the command below −

'log' => env('APP_LOG',’daily’),
登录后复制
  • If thedailylog mode is taken as the parameter, Laravel takes error log for a period of5 days, by default. If you wish to change the maximum number of log files, you have to set the parameter oflog_max_filesin the configuration file to a desired value.

‘log_max_files’ => 25;
登录后复制

Severity Levels

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

'log_level' => env('APP_LOG_LEVEL', 'error')
登录后复制

以上是Laravel - 错误处理的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!