The request parameter laravel herder path corresponding parameter catch

Reference from: https://58hualong.cn/blog/post/laravel-shiyong-terminate-jilu-api-de-xiangying-shijian-qingqiu-canshu-he-xiangyingzhi

Laravel has called middleware  terminate approach, if we implement this method, then the execution is about to end, Laravel Laravel will execute this method throughout the life cycle, the method is structured as follows:

public function terminate($request, $response)
    {
    }

It has two parameters, one is  $request the other one  $response . I.e. this request parameters and the response value. We can use this method to the processing time of the API request parameters and the response value.

A definition of middleware

<?php

namespace App\Http\Middleware;

use App\Jobs\DeleteRedisToken;
use App\Models\ModifyUsersPassword;
use App\Models\User;
use Auth;
use Closure;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;
use \App\Models\Api\LoginToken;
/**
 *
 * - author llc
 *
 */
class AppRequestLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        

        return $next($request);

    }

    public function terminate($request, $response)
    {
        $startTime = LARAVEL_START * 10000;
        $endTime = microtime(true) * 10000;
        //将所有请求写日志
        $url = $request->getRequestUri();
        $header = $request->header();
        $allRequest = $request->all();
        $allInfo = [
            'input' => json_encode($request->input()),
            'response' => $response->getContent(),
            'url' => $url,
            'header' => $header,
            'allRequest' => $allRequest,
            'microtime' => microtime(),
            'start_time' => $startTime,
            'end_time' => $endTime,
            'handle_time' => $endTime - $startTime,
        ];
        Log::useDailyFiles(storage_path('logs/appRequest/appRequestLog.log'), 180, 'debug');
        Log::info('appRequestLog',$allInfo);

    }

}

Kernel.php introduced in the api, the current you can also be applied to other places,

Modified $ middlewareGroups inside

  'api' => [
            \App\Http\Middleware\AppRequestLog::class, //引入中间件
//             \App\Http\Middleware\AppAllSingleLogin::class,
            'throttle:1000,1',
            'bindings',
        ],

Such requests will go through here all the api

After completing its own view files in storage, all the information has been recorded

logs/appRequest/appRequestLog.log
Published 263 original articles · won praise 46 · views 370 000 +

Guess you like

Origin blog.csdn.net/qq_27229113/article/details/103476847