当前位置:首页 > PHP > 正文内容

tp6请求日志,tp6记录详细日志

高老师4年前 (2021-07-07)PHP5375

在tp5版本的时候日志中保存了全部的请求信息,保存了请求地址 请求方法 请求路由  请求头  请求参数,但是在tp6中官方取消了。官方解释说由于日志记录了所有的运行错误,因此养成经常查看日志文件的习惯,可以避免和及早发现很多的错误隐患。但是我觉得不方便我定位线上问题,于是把tp5源码中的部分移植到tp6中,tp5中大部分放在tp底层,为了不破坏tp框架我把代码放到中间件中进行继承,所有中间件全部继承此代码。自动记录请求信息。

<?php
declare (strict_types=1);

namespace app\middleware;

use think\facade\Log;
use think\Request;

class Base
{
    /**
     * 保存请求信息
     * Base constructor.
     */
    public function __construct()
    {
        $request = app(Request::class);
        $requestInfo = [
            'ip' => $request->ip(),
            'method' => $request->method(),
            'host' => $request->host(),
            'uri' => $request->url(),
        ];
        $logInfo = [
            "{$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}",
            '[ ROUTE ] ' . var_export($this->getRouteInfo(), true),
            '[ HEADER ] ' . var_export($request->header(), true),
            '[ PARAM ] ' . var_export($request->param(), true),
            '---------------------------------------------------------------',
        ];
        $logInfo = implode(PHP_EOL, $logInfo) . PHP_EOL;
        Log::record($logInfo, 'info');
    }

    /**
     * 获取路由信息
     * @return array
     */
    protected function getRouteInfo(): array
    {
        $request = app(Request::class);
        return [
            'rule' => $request->rule()->getRule(),
            'route' => $request->rule()->getRoute(),
            'option' => $request->rule()->getOption(),
            'var' => $request->rule()->getVars(),
        ];
    }
}

以上代码从Tp5查看源码实现的。所有中间件都继承base即可。生成的日志和tp5完全一样

[2021-07-07T21:09:50+08:00][info] 127.0.0.1 POST learn.cn/api/v3/up_professions

[ ROUTE ] array (

  'rule' => 'v3/up_professions',

  'route' => 'api/User/update',

  'option' => 

  array (

    'remove_slash' => false,

    'merge_rule_regex' => false,

    'middleware' => 

    array (

      0 => 'app\\middleware\\CheckLogin',

    ),

  ),

  'var' => 

  array (

  ),

)

[ HEADER ] array (

  'accept-encoding' => 'gzip, deflate',

  'accept' => '*/*',

  'token' => 'fjsfnsjdfneofnjsnsfgjnsga',

  'content-type' => 'application/json',

  'content-length' => '2',

  'connection' => 'keep-alive',

  'host' => 'learn.cn',

)

[ PARAM ] array (

)

---------------------------------------------------------------


扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/204.html

分享给朋友:

“tp6请求日志,tp6记录详细日志” 的相关文章

PHP生成缩略图

PHP生成缩略图

//参数1  文件名    参数2  缩放比例  function   _thumb($_filename,$_percent){    ob_clean();...

全方位认识PHP的SESSION

全方位认识PHP的SESSION

【一】.介绍session由于HTTP是无状态的请求,创建一个会话需要保持必须需要身份标识。当用户第一次访问,PHP会为用户创建一个唯一的sessionid,并将sessionid通过cookie发送给浏览器,并在服务器的临时文件创建一个以sessionid为名的文件用来保存这个sessionid保...

PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl.dll找不到指定的模块

PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl.dll找不到指定的模块

最近在编写windows php多线程的东西,从官网下载了PHP的线程安全版,尝试开启curl扩展extension=php_curl.dllphp -m 却提示 PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl...

php下载远程文件(支持断点续传,支持超大文件)

php下载远程文件(支持断点续传,支持超大文件)

断点下载的原理:http请求头添加Range参数告诉文件服务器端需要的字节范围例如1个文本文件的字节为1000,第一次请求Range: bytes=0-500第二次请求Range: bytes=501-1000通过每次的请求将返回的流追加写入到文件。注意的项目:断点下载服务器端的每次只返回字节传输的...

php端口复用,php socket端口复用

php端口复用,php socket端口复用

第一次听说端口复用是在mixphp最新版本中发现的,mixphp启动监听9501端口,现在作者说可以多开几个进程来执行mixphp,我心里想了下再启动不是会端口冲突嘛,但是却没有问题,于是下载mixphp的源码解读,原来是启动http服务器使用new Co\Http\Server('0.0....

php生成器的send方法详解,php yield send

php生成器的send方法详解,php yield send

【一】.基本用法首先看看官方对send方法的解释:Sets the return value of the yield expression and resumes the generato...