Detailed explanation of errors and log usage in laravel _ php instance

Source: Internet
Author: User
This article mainly introduces errors and log usage in laravel, and analyzes in detail the settings and usage skills of errors and logs in laravel in combination with the instance form, for more information about laravel errors and log usage, see the examples in this article. We will share this with you for your reference. The details are as follows:

Logs

Logs in laravel are encapsulated based on monolog. Laravel has done several things on it:

① Simplify addInfo and other functions in monolog into functions such as info

② The useFiles and useDailyFiles parameters are added to make log management and cutting easier.

③ To call the monolog method, call the callMonolog function.

Now, let's see how to implement the following requirements:

Store different log information in different logs

This requirement is common. for example, the order calling log must be recorded in order. log, and the store information record must be recorded in shop. log. You can do this:

<? Php use Monolog \ Logger; use Monolog \ Handler \ StreamHandler; use Illuminate \ Log \ Writer; class BLogger {// const LOG_ERROR = 'error' must be registered here for all logs '; private static $ loggers = array (); // Get an instance public static function getLogger ($ type = self: LOG_ERROR, $ day = 30) {if (empty (self :: $ loggers [$ type]) {self: $ loggers [$ type] = new Writer (new Logger ($ type); self :: $ loggers [$ type]-> useDailyFiles (storage_path (). '/logs /'. $ type. '. log', $ day) ;}$ log = self: $ loggers [$ type]; return $ log ;}}

In this way, different log data will be stored in different log files. It can also record log data information.

Laravel's error log stack is too long. what should I do?

Use the above BLogger class to record the necessary error information in start/global. php

// Error Log information App: error (function (Exception $ exception, $ code) {Log: error ($ exception ); $ err = ['message' => $ exception-> getMessage (), 'file' => $ exception-> getFile (), 'line' => $ exception-> getLine (), 'code' => $ exception-> getCode (), 'URL' => Request: url (), 'input' => input: all (),]; BLogger: getLogger (BLogger: LOG_ERROR)-> error ($ err );});

Laravel default logs are not separated

Therefore, the default log records of laravel should be changed to segmented by default.

Also in start/global. php

Log::useDailyFiles(storage_path().'/logs/laravel.log', 30);

How to record the SQL log of a request

This should be further detailed. do you want to record it in real time?

If real-time records are not required, laravel has a DB: getQueryLog that can be used to obtain an SQL request from an app request:

# In filters. php App: after (function ($ request, $ response) {// database query logs $ queries = DB: getQueryLog (); if (Config :: get ('query. log', false) {BLogger: getLogger ('query')-> info ($ queries );}}

If you need to record it in real time (that is, when you die out from anywhere, the SQL requests on the previous page are also recorded), you need to listen to the illuminate. query event.

// If (Config: get ('database. log', false) {Event: listen ('illuminate. query', function ($ query, $ bindings, $ time, $ name) {$ data = compact ('query', 'binnings', 'time', 'name '); BLogger: getLogger (BLogger: LOG_QUERY_REAL_TIME)-> info ($ data );});}

Error

All laravel errors will be returned after the global App: error.

For example, if you design an interface and want to return json data even if an error occurs, you can do this:

// Error log information App: error (function (Exception $ exception, $ code) {// directly jump to the logon page if no path exists ($ exception instanceof NotFoundHttpException) {return Redirect: route ('login');} Log: error ($ exception); $ err = ['message' => $ exception-> getMessage (), 'file' => $ exception-> getFile (), 'line' => $ exception-> getLine (), 'code' => $ exception-> getCode (), 'URL' => Request: url (), 'input' => input: all (),]; BLogger: getLogger (BLogger: LOG_ERROR) -> error ($ err); $ response = ['status' => 0, 'error' => "Internal Server error",]; return Response :: json ($ response );});

If you want to hold the 404 error:

App: missing (function ($ exception) {$ response = ['status' => 0, 'error' => "request path error",]; return Response :: json ($ response );});

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.