In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the error log of Laravel 5.3". In daily operation, I believe many people have doubts about how to understand the error log of Laravel 5.3.The editor consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to understand the error log of Laravel 5.3". Next, please follow the editor to study!
1. Introduction
Laravel has configured errors and exception handling for us by default, and we trigger an exception in the App\ Exceptions\ Handler class and return the response to the user. We will explore this class in depth in this tutorial.
In addition, Laravel also integrates the Monolog log library to provide a variety of powerful log processors. By default, Laravel has configured some processors for us, and we can select a single log file or choose to log error messages to the system log.
2. Configuration
Error details are displayed
The debug configuration item in the configuration file config/app.php controls the number of error details displayed by the browser. By default, this configuration item is set by the environment variable APP_DEBUG in the .env file.
For local developers, you should set the environment variable APP_ debug to true. In a production environment, this value should be set to false. If it is set to true in a production environment, it is possible to expose some sensitive configuration values to the end user.
Log storage
By default, Laravel supports the logging methods single, daily, syslog, and errorlog. If you want log files to be generated on a daily basis rather than a single file, you should set the log value in the configuration file config/app.php as follows:
'log' = >' daily'
Maximum life cycle of log files
When using daily log mode, Laravel keeps logs for us for the last 5 days by default. If you want to modify this time, you need to add a configuration log_max_files to the app configuration file:
'log_max_files' = > 30
Log error level
When using Monolog, log messages may have different error levels. By default, Laravel writes all logs to the storage directory, but in a production environment, you may want to configure the lowest error level, which can be achieved by adding the configuration item log_level to the configuration file app.php.
After the configuration item is configured, Laravel will log all logs with an error level greater than or equal to this specified level. For example, if the default log_level is error, log information at error, critical, alert, and emergency levels will be recorded:
'log_level' = > env (' APP_LOG_LEVEL', 'error')
Note: Monolog supports the following error levels-debug, info, notice, warning, error, critical, alert, emergency.
Customize Monolog configuration
If you want to have complete control over the configuration of Monolog in your application, you can use the configureMonologUsing method. You need to call this method before the bootstrap/app.php file returns the $app variable:
$app- > configureMonologUsing (function ($monolog) {$monolog- > pushHandler (...);}); return $app
3. Exception handler
All exceptions are handled by the class App\ Exceptions\ Handler, which contains two methods: report and render. Let's elaborate on these two methods.
Report method
The report method is used to record the exception and send it to an external service such as Bugsnag or Sentry. By default, the report method only passes the exception to the base class where the exception is logged. Of course, you can also record the exception and handle it as needed.
For example, if you need to report different types of exceptions in different ways, use PHP's instanceof comparison operator:
/ * * report or record exceptions * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * @ param\ Exception $e * @ return void * / public function report (Exception $e) {if ($e instanceof CustomException) {/ /} return parent::report ($e);}
Ignore exceptions by type
The $dontReport attribute of the exception handler contains an array of exception types that will not be logged. By default, 404 error exceptions are not written to the log file, and you can add other exception types to this array if necessary:
/ * A list of the exception types that should not be reported. * * @ var array * / 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,]
Render method
The render method is responsible for converting the given exception into a HTTP response sent to the browser, and by default, the exception is passed to the base class that generated the response for you. Of course, you can also check the exception type or return a custom response according to your needs:
/ * render the exception to the HTTP response * * @ param\ Illuminate\ Http\ Request $request * @ param\ Exception $e * @ return\ Illuminate\ Http\ Response * / public function render ($request, Exception $e) {if ($e instanceof CustomException) {return response ()-> view ('errors.custom', [], 500);} return parent::render ($request, $e);}
4. HTTP exception
Some exceptions describe the HTTP error code from the server. For example, this may be a "page not found" error, an "authentication failure error", or a 500th error caused by a program error. To generate such a response in the application, you can use the abort method:
Abort (404)
The abort method immediately throws an exception that will be rendered by the exception handler. In addition, you can provide a response description like this:
Abort (403, 'Unauthorized action.')
This method can be used at any point in the request life cycle.
Customize the HTTP error page
In Laravel, it is easy to return error pages with different HTTP status codes, for example, if you want to customize the 404 error page, create a resources/views/errors/404.blade.php file that will be used to render all 404 errors returned by the program. It is important to note that the view names in this directory should match the corresponding HTTP status codes.
5. Log
Laravel provides a simple log abstraction layer based on the powerful Monolog library. By default, Laravel is configured to generate log files for applications every day under the storage/logs directory. You can use Log facade to record log information to the log:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.