首页 > 其他分享 >webman:全局中间件:记录访问日志(v1.5.7)

webman:全局中间件:记录访问日志(v1.5.7)

时间:2023-08-23 10:01:46浏览次数:44  
标签:webman request middleware 中间件 v1.5 https php com

一,官方文档地址:

https://www.workerman.net/doc/webman/middleware.html

二,php代码

1,配置中间件:

config/middleware.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php /**  * This file is part of webman.  *  * Licensed under The MIT License  * For full copyright and license information, please see the MIT-LICENSE.txt  * Redistributions of files must retain the above copyright notice.  *  * @author    walkor<[email protected]>  * @copyright walkor<[email protected]>  * @link      http://www.workerman.net/  * @license   http://www.opensource.org/licenses/mit-license.php MIT License  */   return [     '' => [         app\middleware\HttpLog::class,     ] ];

2,中间件:

app/middleware/HttpLog.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <?php namespace app\middleware;   use Webman\MiddlewareInterface; use Webman\Http\Response; use Webman\Http\Request; use support\Log;   class HttpLog implements MiddlewareInterface {     public function process(Request $request, callable $handler) : Response     {         echo '这里是请求穿越阶段,也就是请求处理前';         //开始时间         $startTime = microtime(true);           $response = $handler($request); // 继续向洋葱芯穿越,直至执行控制器得到响应           echo '这里是响应穿出阶段,也就是请求处理后';         //响应时间         $endTime = microtime(true);         //共计用时         $costTime = $endTime - $startTime;           //写访问日志,指定通道         //47.92.78.77 - - [20/Aug/2023:02:08:45 +0800] "GET / HTTP/1.1" 200 13364 "-" "Apache-HttpClient/5.1.3 (Java/1.8.0_342)"            $log = Log::channel('httpLog');          $ip = $request->getRemoteIp();   //ip          $time = date("Y-m-d H:i:s");     //访问时间          $method = $request->method();    //method          $url = $request->fullUrl();      //url          $version = $request->protocolVersion();  //http的版本          $status = $response->getStatusCode();    //返回时的状态码          $size = strlen($response->rawBody());    //内容大小          $agent = $request->header('user-agent'); //ua          //写日志          $content = $ip." ".$time." ".$method." ".$url." ".$version." ".$status." ".$size." ".$costTime." ".$agent;          $log->info($content);           return $response;     } }

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/21/webman-quan-ju-zhong-jian-jian-ji-lu-fang-wen-ri-zhi-v1-5-7/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

三,测试效果:

查看日志文件中的记录:

[2023-08-20 22:09:07] httpLog.INFO: 192.168.219.1 2023-08-20 22:09:07 GET //192.168.219.6:8787/image/list 1.1 200 1328 0.0021569728851318 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 [] []

四,查看webman版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name     : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...

标签:webman,request,middleware,中间件,v1.5,https,php,com
From: https://www.cnblogs.com/architectforest/p/17650346.html

相关文章

  • webman:用thinkorm访问数据库(v1.5.7)
    一,官方文档地址:https://www.workerman.net/doc/webman/db/thinkorm.html二,安装组件liuhongdi@lhdpc:/data/webman/imageadmin$composerrequire-Wwebman/think-orm./composer.jsonhasbeenupdatedRunningcomposerupdatewebman/think-orm--with-all-dependencies......
  • webman:用thinkcache访问redis(v1.5.7)
    一,官方文档地址:https://www.workerman.net/doc/webman/db/thinkcache.html二,安装组件liuhongdi@lhdpc:/data/webman/imageadmin$composerrequire-Wwebman/think-cache三,配置redisconfig/thinkcache.php,按自己的实际情况配置12345678910111213......
  • webman:配置异常处理返回json格式(v1.5.7)
    一,添加一个除0错的异常代码:页面显示效果如图:二,配置:php代码1,config/123456789101112131415161718<?php/** *Thisfileispartofwebman. * *LicensedunderTheMITLicense *Forfullcopyrightandlicenseinformation......
  • 了解ASP.NET Core中的中间件概念
    当谈到构建强大且高度可定制的Web应用程序时,ASP.NETCore是一个备受推崇的选择。其灵活性和可扩展性使开发人员能够以更有创意的方式构建应用程序,其中的中间件概念是实现这种灵活性的关键。什么是中间件?中间件是ASP.NETCore应用程序处理HTTP请求和响应的组件。它们在请求到达应用......
  • Django自定义中间件验证用户token信息
    1.新建middleware.pyfromdjango.urlsimportreversefromrest_framework.responseimportResponsefromutils.tokenimportcheck_tokenfromdjango.httpimportJsonResponse,HttpResponseRedirectfromyshop.modelsimportMyUsertry:fromdjango.utils.de......
  • Express - 中间件
    中间件编写中间件函数可以接受三个参数,req(请求),res(响应)和next(下一个中间件函数)。如果这个中间件不结束请求/响应循环,就需要调用next函数。先装载的中间件函数会被先执行,如果忘记调用next函数,会导致后装载的中间件被忽略。使用中间件应用层中间件可以使用app.use()和app.MET......
  • webman:安装/创建项目(v1.5.7)
    一,官方文档:1,官方站:https://www.workerman.net/webman2,安装文档:https://www.workerman.net/doc/webman/install.html二,准备安装环境:1,需求环境需求PHP>=7.2Composer >=2.02,查看本地环境:php:liuhongdi@lhdpc:~$/usr/local/soft/php8/bin/php--version......
  • webman:修改默认页面(v1.5.7)
    一,默认页面的内容:说明:代码位于app/IndexController.php参考这个文档:https://www.workerman.net/doc/webman/route.html原始代码:显示了README.md这个文件的内容12345678910classIndexController{    publicfunctionindex(Request$reques......
  • webman:管理命令(v1.5.7)
     一,启动和停止1,启动#-d:以daemon方式启动,用于生产环境liuhongdi@lhdpc:/data/webman/imageadmin$phpstart.phpstart-dWorkerman[start.php]startinDAEMONmode-------------------------------------------WORKERMAN--------------------------------------......
  • webman:配置路由(v1.5.7)
     一,官方文档地址:https://www.workerman.net/doc/webman/route.html二,php代码:config/route.php1234567891011121314151617181920<?php useWebman\Route; //指定默认页面Route::get('/',[app\controller\IndexController::class......