[Laravel 5.2 Documentation] Service-Error & Log

Source: Internet
Author: User

1. Introduction

Laravel the error and exception handling has been configured for us by default, and Laravel also integrates the Monolog log Library to provide a variety of powerful log processors.

2. Configuration

Error details display

The Debug configuration option in Profile config/app.php controls the number of error details displayed by the browser. By default, this configuration option is set to the environment variable app_debug in the. env file.

For local development, you should set the environment variable App_debug value to True. In a production environment, the value should be set to false.

Log mode

Laravel supports log methods single, daily, Syslog, and errorlog. For example, if you want the log file to be generated on a daily basis instead of generating a single file, you should set the log value in the configuration file config/app.php as follows:

' Log ' = ' daily '

Customizing the Monolog Configuration

If you want to fully control the configuration of Monolog in your app, you can use the Configuremonologusing method of the app. You should call this method before the bootstrap/app.php file returns $app variable:

$app->configuremonologusing (function ($monolog) {    $monolog->pushhandler (...);}); return $app;

3. Exception Processor

All exceptions are handled by Class App\exceptions\handler, which consists of two methods: report and Render. Let's elaborate on these two methods.

3.1 Report method

The report method is used to log exceptions and send them to external services such as Bugsnag. By default, the report method simply passes the exception to the base class where the exception is recorded, and you can record the exception as you like.

For example, if you need to report different types of exceptions in different ways, you can use the PHP instanceof comparison operator:

/** * Report or Record exception * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param  \exception  $e * @ret Urn void */public function report (Exception $e) {    if ($e instanceof customexception) {        //    }    return Parent :: report ($E);}

Ignoring exceptions by type

The $dontReport attribute of the exception handler contains an array of exception types that will not be logged, and by default the 404 error exception will not be written to the log file, and you can add additional exception types to this array, if necessary.

3.2 Render Method

The Render method is responsible for translating the given exception into the HTTP response sent to the browser, by default, the exception is passed to the base class that generated the response for you. However, you can check the exception type as you wish or return a custom response:

/** * Render exception to 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 ', [], +);    }    Return Parent::render ($request, $e);}

4. HTTP Exception

Some exceptions describe HTTP error codes from the server, for example, this could be a "page not found" error (404), "Authentication Failure Error" (401) or a 500 error caused by a program error, in order to generate such a response in the application, use the following method:

Abort (404);

The Abort method immediately throws an exception that will be rendered by the exception handler, and 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.

Custom HTTP Error page

Laravel makes it easy to return error pages with multiple 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 render all of the programs generated by the program 404 error.

The name of the view under the directory should match the corresponding HTTP status code.

5. Log

The Laravel log tool is based on a powerful Monolog library, and by default, Laravel is configured to generate log files for the app daily in the Storage/logs directory, and you can write log information to the log using the log façade:

 
  User::findorfail ($id)]);}    }

The logger provides the eight logging levels defined in RFC 5424: emergency, alert, critical, error, warning , notice, info and debug.

Log::emergency ($error); Log::alert ($error); Log::critical ($error); Log::error ($error); Log::warning ($error); Log::notice ($error); Log::info ($error); Log::d ebug ($error);

Contextual information

The context data array is also passed to the log method. The context data will be formatted and displayed along with the log message:

Log::info (' User failed to login. ', [' id ' = ' = $user->id]);

Accessing the underlying Monolog instance

Monolog has several processors available for logging, and if necessary, you can access the underlying Monolog instances:

$monolog = Log::getmonolog ();
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.