Laravel框架实现利用监听器进行sql语句记录功能_php实例_脚本之家

Posted by

本文实例讲述了Laravel框架实现利用中间件进行操作日志记录功能。分享给大家供大家参考,具体如下:

本文实例讲述了Laravel框架实现利用监听器进行sql语句记录功能。分享给大家供大家参考,具体如下:

利用中间件进行操作日志记录过程:

利用监听器进行sql语句记录

1、创建中间件

365bet亚洲平台,1、监听sql语句的事件类已经定义,直接创建监听器类即可:

php artisan make:middleware AdminOperationLog
# 监听sqlmake:listener QueryListener --event=IlluminateDatabaseEventsQueryExecuted

2、生成了文件./app/Http/Middleware/AdminOperationLog.php

2、监听器类代码

method{ $input = $request->all(); $log = new OperationLog(); # 提前创建表、model $log->uid = $user_id; $log->path = $request->path(); $log->method = $request->method(); $log->ip = $request->ip(); $log->sql = ''; $log->input = json_encode($input, JSON_UNESCAPED_UNICODE); $log->save(); # 记录日志 } return $next; }}

./app/Listeners/QueryListener.php

3、中间件引入 ./app/Http/Kernel.php

sql); $log = vsprintf($sql, $event->bindings); # 此处$uid定义是依赖于中间件记录操作日志代码 $uid = isset($_SERVER['admin_uid']) ? $_SERVER['admin_uid'] : 0; if('select' != substr{ if('insert into `operationLog`' != substr{ $OperationLog = new OperationLog(); $OperationLog->uid = $uid; $OperationLog->sql = $log; $OperationLog->input = ''; $OperationLog->save(); } } }}
protected $middlewareGroups = [ 'web' => [ ... AppHttpMiddlewareAdminOperationLog::class, ... ], 'api' => [ 'throttle:60,1', 'bindings', ], ];

3、引入监听器

相关文章

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注