ezijing / hyperf-annotated-log
hyperf 注解日志
v1.1.9
2022-01-06 09:20 UTC
Requires
- php: >=7.2
- ext-json: *
- ext-swoole: >=4.5
- hyperf/config: 2.2.*
- hyperf/database: 2.2.*
- hyperf/db-connection: 2.2.*
- hyperf/di: 2.2.*
- hyperf/framework: 2.2.*
- hyperf/snowflake: 2.2.*
- hyperf/utils: 2.2.*
- zhuzhichao/ip-location-zh: ^2.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- hyperf/devtool: ~2.2.0
- hyperf/ide-helper: ~2.2.0
- hyperf/testing: ~2.2.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^0.12
- swoole/ide-helper: ^4.5
Suggests
- ext-json: Required to use JSON.
- ext-openssl: Required to use HTTPS.
- ext-pdo: Required to use MySQL Client.
- ext-pdo_mysql: Required to use MySQL Client.
- ext-redis: Required to use Redis Client.
README
注意:当前日志的数据存储只支持mysql
使用说明
1. 先下载composer包
composer require ezijing/hyperf-annotated-log -vvv
发布
php bin/hyperf.php vendor:publish ezijing/hyperf-annotated-log
2. 执行数据库脚本
- 脚本路径
./vendor/ezijing/hyperf-annotated-log/db/log.sql
CREATE TABLE `s_logs` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `ip` varchar(50) NOT NULL DEFAULT '' COMMENT 'ip地址', `operate` varchar(255) NOT NULL DEFAULT '' COMMENT '操作说明', `desc` text NOT NULL COMMENT '操作内容', `operator` varchar(50) NOT NULL DEFAULT '' COMMENT '操作者', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_ip` (`ip`), KEY `idx_operate` (`operate`), KEY `idx_operator` (`operator`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='操作日志表';
3. 配置操作用户
用户登录成功获取用户信息后进行设置
return [ // 配置操作者的ID // 登录用户信息存储在 Context下 'user' => [ 'key' => 'user', // context的key 'id' => 'id', // 用户的id ], ];
$loginUser = user(); // 获取登录信息伪代码 Context::set('annotation_log.user.key', $loginUser);
4. 注解操作示例
4.1 注意:
- 目前只支持控制器中的Action进行注解
- 注解到其他方法中也可以;但无法获取到方法传递的动态参数。
4.2示例:
<?php declare(strict_types=1); namespace App\Controller; use App\Services\UserService; use Ezijing\HyperfAnnotatedLog\Annotations\OperationLog; use Hyperf\Di\Annotation\Inject; class IndexController extends AbstractController { /** * @Inject * @var UserService */ protected $service; /** * @OperationLog(operation="test", desc="测试") */ public function index() { $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); return [ 'method' => $method, 'message' => "Hello {$user}.", ]; } }