Laravel error and log usage _php instance

Source: Internet
Author: User
Tags smarty template
The examples in this article describe errors and log usage in Laravel. Share to everyone for your reference, as follows:

Log

The logs in Laravel are encapsulated based on Monolog. Laravel did a few things on it:

① simplifies the functions of addinfo in Monolog into functions such as info.

② adds two parameters for Usefiles and usedailyfiles, making log management and cutting easier.

③ Call the Callmonolog function if you want to call the Monolog method

OK, look at the following several requirements how to achieve:

Store different log information in a different log

This demand is very common, such as the call order of the log, need to record to Order.log, access to store information records need to be recorded in the Shop.log. You can do this:

<?php use Monolog\logger;use monolog\handler\streamhandler;use illuminate\log\writer;class BLogger{  // All LOG requests are here to register  const LOG_ERROR = ' ERROR ';  private static $loggers = Array ();  Gets an instance  of public static function GetLogger ($type = self::log_error, $day = +)  {    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;  }}

This allows different log data to be stored in different log files. Log data information can also be logged.

Laravel the error log stack is too long, what do I do?

Use the above Blogger class to record the necessary error messages in the 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 The default log does not use split

Therefore, the default log record of Laravel should be changed to be segmented by default.

Also in the start/global.php.

Log::usedailyfiles (Storage_path (). ' /logs/laravel.log ', 30);

How to log a request for SQL logs

This should be refined to ask, do you want to record in real time?

If you do not log in real time, then Laravel has a db::getquerylog to get an app request to get the SQL request:

# # in filters.php app::after (function ($request, $response) {  //database query for log  $queries = Db::getquerylog ();  if (Config::get (' Query.log ', false)) {    Blogger::getlogger (' query ')->info ($queries);}  }

If you need to record in real time (that is, when you die from anywhere, the SQL request for the previous page is recorded), you need to listen to the Illuminate.query event.

Database real-time request log if (Config::get (' Database.log ', false)) {  event::listen (' Illuminate.query ', function ($query, $ Bindings, $time, $name)  {    $data = compact (' query ', ' bindings ', ' time ', ' name ');    Blogger::getlogger (blogger::log_query_real_time)->info ($data);  });

Error

All of Laravel's errors will come out of the app::error of Global.

So, for example, you're designing an interface, and you want to return JSON data even if an error occurs, you can do this:

Error log Information App::error (function (Exception $exception, $code) {  //If there is no path, jump directly to the login page if  ($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 ' = ' Server internal error ',  ];  Return Response::json ($response);});

If you also want to hold the 404 error:

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

More interested in laravel related content readers can view this site topic: "Laravel Framework Introductory and Advanced tutorial", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction tutorial "," PHP String Usage Summary "," Introduction to Php+mysql Database Operations "and" PHP common database Operation Skills Summary "

It is hoped that this article is helpful to the PHP program design based on Laravel framework.

  • 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.