Home  >  Article  >  PHP Framework  >  About errors and logs in laravel

About errors and logs in laravel

藏色散人
藏色散人Original
2021-04-09 13:46:332260browse

The following tutorial column will introduce you to the errors and logs in laravel (you can customize the log directory and log file name). I hope it will be helpful to friends in need!

About errors and logs in laravel

Log

The log in laravel is encapsulated based on monolog. laravel has done several things on it:

Simplified functions such as addInfo in monolog into functions like info

Added two parameters useFiles and useDailyFiles, making it possible to do Log management and cutting have become easier
  • If you want to call the monolog method, you need to call the callMonolog function
  • Okay, let’s see how to implement the following requirements:
  • Store different log information in different logs

This requirement is very common. For example, the log of calling an order needs to be recorded in order.log, and the record of obtaining store information needs to be recorded in shop.log. go. You can do this:

<?php 
 
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Illuminate\Log\Writer;
 
class BLogger
{
    // 所有的LOG都要求在这里注册
    const LOG_ERROR = &#39;error&#39;;
 
    private static $loggers = array();
 
    // 获取一个实例
    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().&#39;/logs/&#39;. $type .&#39;.log&#39;, $day);
        }
 
        $log = self::$loggers[$type];
        return $log;
    }
}

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

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

// 错误日志信息
App::error(function(Exception $exception, $code)
{
    Log::error($exception);
 
    $err = [
        &#39;message&#39; => $exception->getMessage(),
        &#39;file&#39; => $exception->getFile(),
        &#39;line&#39; => $exception->getLine(),
        &#39;code&#39; => $exception->getCode(),
        &#39;url&#39; => Request::url(),
        &#39;input&#39; => Input::all(),
    ];
    BLogger::getLogger(BLogger::LOG_ERROR)->error($err);
});

Laravel’s default log does not use segmentation

So laravel’s default logging should be changed to split 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 more detailed Hua asked, do you want to record in real time? If you don’t want real-time recording, then laravel has DB::getQueryLog to get the sql request obtained by an app request:

## 在filters.php中
App::after(function($request, $response)
{
    // 数据库查询进行日志
    $queries = DB::getQueryLog();
    if (Config::get(&#39;query.log&#39;, false)) {
        BLogger::getLogger(&#39;query&#39;)->info($queries);
    }
}

If you need real-time recording (that is, you are in any When the local die comes out, the sql request of the previous page is also recorded), you need to monitor the illuminate.query event

// 数据库实时请求的日志
if (Config::get(&#39;database.log&#39;, false))
{
    Event::listen(&#39;illuminate.query&#39;, function($query, $bindings, $time, $name)
    {
        $data = compact(&#39;query&#39;,&#39;bindings&#39;, &#39;time&#39;, &#39;name&#39;);
        BLogger::getLogger(BLogger::LOG_QUERY_REAL_TIME)->info($data);
    });
}

Error

laravel All errors will pass through the global App::error before appearing. So if you design an interface and hope that json data will be returned even if an error occurs, you can do this:

// 错误日志信息
App::error(function(Exception $exception, $code)
{
    // 如果没有路径就直接跳转到登录页面
    if ($exception instanceof NotFoundHttpException) {
        return Redirect::route(&#39;login&#39;);
    }
 
    Log::error($exception);
 
    $err = [
        &#39;message&#39; => $exception->getMessage(),
        &#39;file&#39; => $exception->getFile(),
        &#39;line&#39; => $exception->getLine(),
        &#39;code&#39; => $exception->getCode(),
        &#39;url&#39; => Request::url(),
        &#39;input&#39; => Input::all(),
    ];
    BLogger::getLogger(BLogger::LOG_ERROR)->error($err);
 
    $response = [
        &#39;status&#39; => 0,
        &#39;error&#39; => "服务器内部错误",
    ];
    return Response::json($response);
});

If you still want to hold the 404 error:

App::missing(function($exception)
{
    $response = [
        &#39;status&#39; => 0,
        &#39;error&#39; => "请求路径错误",
    ];
    return Response::json($response);
});

The above is the detailed content of About errors and logs in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn