首页 > 其他分享 >laravel: 用中间件把请求和响应log下来

laravel: 用中间件把请求和响应log下来

时间:2024-08-07 17:29:48浏览次数:11  
标签:laravel log 中间件 resLog request 万达 array

一,代码:

1, 中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\extend\LogEs;

class LogSearch
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        //只记录request的话,代码可以写在这里
        $response = $next($request);
        //log request
        $request = [
            'url' => $request->fullUrl(),
            'method' => $request->method(),
            'input' => $request->input(),
        ];
        //把响应的content转为数组记录,如果是object不能直接log
        $resLog = json_decode($response->getContent());
        $resLog = (array)$resLog;
        LogEs::logSearch($request,$resLog);
        //返回
        return $response;
    }
}

2,routes

Route::controller(SouController::class)->middleware([LogSearch::class])->group(function () {
    Route::post('/sou/souershou', 'souershou');
});

3,log类

<?php
namespace App\extend;

use App\Business\BaseBusiness;
use Illuminate\Support\Facades\Log;

class LogEs extends BaseBusiness
{
     //写搜索的日志
     static public function logSearch($arr_request,$arr_response) {
        Log::channel('essearch')->info($arr_request);
        Log::channel('essearch')->info($arr_response);
    }
}

 

二,测试效果:

[2024-08-07 15:48:59] local.INFO: array (
  'url' => 'http://192.168.219.6/api/soud/souershou?wushi%5B0%5D=1&wushi%5B1%5D=3&city=%E5%8C%97%E4%BA%AC&keystr=%E4%B8%87%E8%BE%BE&order=areaasc&p=1&zhuangxiu%5B0%5D=1',
  'method' => 'POST',
  'input' => 
  array (
    'city' => '北京',
    'keystr' => '万达',
    'order' => 'areaasc',
    'wushi' => 
    array (
      0 => '1',
      1 => '3',
    ),
    'zhuangxiu' => 
    array (
      0 => '1',
    ),
    'p' => '1',
  ),
)  
[2024-08-07 15:48:59] local.INFO: array (
  'status' => 'success',
  'code' => 200,
  'time' => '2024-08-07 15:48:59',
  'msg' => '',
  'data' => 
  (object) array(
     'total' => 1,
     'data' => 
    array (
      0 => 
      (object) array(
         'f_id' => 6,
         'f_type' => 1,
         'f_district' => '通州区',
         'f_cover_image' => NULL,
         'f_area' => '55㎡',
         'f_toward' => '南',
         'f_title' => '二手房·中仓小区 万达商圈 满五年唯一 6号地铁',
         'f_district' => '万达,通州万达',
      ),
    ),
  ),
)  

三,说明:

生产环境中会影响性能,通常用于测试阶段供调试使用

 

标签:laravel,log,中间件,resLog,request,万达,array
From: https://www.cnblogs.com/architectforest/p/18347491

相关文章

  • go高并发之路——消息中间件kafka(下)
    一、kafka副本机制所谓的副本机制(Replication),也可以称之为备份机制,通常是指分布式系统在多台网络互联的机器上保存有相同的数据拷贝。kafka的副本概念实际上是在分区(partition)层级下定义的,每个分区配置有若干个副本。根据Kafka副本机制的定义,同一个分区下的所有副本保存有相同......
  • 删库了不用跑路!binlog恢复数据实操
    各位道友大家好呀!想必道友们或多或少都听说过MySQL的binlog的作用,它记录了数据库整个的生命周期,可用于恢复数据或者从库同步数据。那么如果发生了数据库误删,具体该怎样恢复数据呢?下面就以一个例子来给道友们演示一下,让我们开始吧!doit!数据备份首先,数据库要定时进行备份,因为如......
  • c# net6创建API项目 日志管理log4net的用法
    一、program.cs//配置log4netXmlConfigurator.Configure(newFileInfo("log4net.config"));二、公共类LogHelper.csnamespaceElecInvoice.Common{publicclassLogHelper{publicstaticreadonlylog4net.ILogloginfo=log4net.LogManage......
  • MySQL 备库可以设置 sync_binlog 非 1 吗?【转】
    众所周知,防止断电丢失Binlog、故障恢复过程丢失数据,MySQL主库必须设置sync_binlog=1。那么作为备库可以例外吗?我们的第一反应当然是不行,既然主库会丢数据,备库自然一样。但其实不然,备库丢了数据是可以重新从主库上复制的,只要这个复制的位置和备库本身数据的位置一致就OK了,它......
  • PostgreSQL学习之pg_recvlogical与pgoutput的使用
        参考:        pg内功修炼:逻辑复制_pgoutput-CSDN博客        PG原生解码工具pg_recvlogical的使用-在脑裂时帮我们找回丢失的数据-腾讯云开发者社区-腾讯云(tencent.com)        postgresql数据库的原生解码插件pg_recvlogical可以将wal......
  • 【项目实战】在 MyBatis Plus 中添加 `@TableLogic` 注解,以实现逻辑删除
    一,需求描述在MyBatisPlus中实现逻辑删除是一种常见的需求逻辑删除,通常用于避免直接从数据库中物理删除数据,而是标记这些数据为“已删除”。逻辑删除,可以通过在表中添加一个额外的字段(如deleted或is_deleted)来实现。逻辑删除,当该字段为某个值时(例如1或者true),表示这......
  • 小白学php,emlog6.0代码审计,fortify代码审计
    一、初识报错注入报错注入是一种通过引发SQL错误并利用错误消息回显数据库信息的技术。通过使用MySQL中的特定函数(如UPDATEXML),可以将数据库查询结果嵌入到错误消息中,从而获取数据库中的数据。二、执行原理代码逻辑分析:用户输入参数(如用户ID)。后台执行SQL查询(查询用户信息......
  • 常见中间件漏洞
    一、Tomcat1.CVE-2017-12615 1.1打开tomcat页面1.2打开burp进行抓包,修改PUT方式,后面加上一句话木马<%!classUextendsClassLoader{U(ClassLoaderc){super(c);}publicClassg(byte[]b){returnsuper.......
  • laravel:得到linux平台当前脚本的执行用户
    一,代码:<?phpnamespaceApp\Console\Commands;useIlluminate\Console\Command;classIndexAllCommandextendsCommand{/***Thenameandsignatureoftheconsolecommand.**@varstring*/protected$signature='......
  • laravel:从linux命令行运行command
    一,创建command1,执行命令liuhongdi@lhdpc:/data/api$phpartisanmake:commandIndexAllCommand2,查看创建的文件:<?phpnamespaceApp\Console\Commands;useIlluminate\Console\Command;classIndexAllCommandextendsCommand{/***Thenameandsignatu......