lyrasoft / action-log
LYRASOFT 操作日志包
1.0.1
2023-12-15 09:27 UTC
Requires
- php: >=8.2
- lyrasoft/luna: ^2.1
- lyrasoft/toolkit: ^1.1
- openspout/openspout: ^4.22
This package is auto-updated.
Last update: 2024-09-15 11:09:01 UTC
README
安装
使用 composer 安装
composer require lyrasoft/action-log
然后,将文件复制到项目中
php windwalker pkg:install lyrasoft/action-log -t routes -t migrations
并运行迁移。
接下来,将中间件添加到 routes/admin.route.php
use Lyrasoft\ActionLog\Middleware\ActionLogMiddleware; // ... $router->group('admin') // ... ->middleware(ActionLogMiddleware::class) // ...
如果您想记录前端和后台,可以将中间件添加到
main.route.php
语言
将此行添加到后台和前端中间件,以自动从包中加载语言
use Lyrasoft\ActionLog\ActionLogPackage; // ... $this->lang->loadAllFromVendor(ActionLogPackage::class, 'ini');
如果您想将语言文件复制到项目中,运行此命令
php windwalker pkg:install lyrasoft/action-log -t lang
注册后台菜单
编辑 resources/menu/admin/sidemenu.menu.php
// Action Log $menu->link('操作記錄') ->to($nav->to('action_log_list')) ->icon('fal fa-shoe-prints');
入门指南
如果您已设置 ActionLog 包,现在尝试保存任何项或过滤任何列表,您可以在 action_logs
表中看到新的日志
自动清理
默认情况下,ActionLog 只保留最后 3 个月的日志。它有 1/100 的机会触发清理操作。
您可以在配置文件中配置保留时间和清理机会,或者使用环境变量进行配置。
return [ 'action_log' => [ 'reserve_max_time' => env('ACTION_LOG_MAX_TIME') ?: '3months', 'auth_clear' => [ 'chance' => env('ACTION_LOG_CLEAR_CHANCE', 1), 'chance_base' => env('ACTION_LOG_CLEAR_CHANCE_BASE', 100) ], // ... ] ];
配置中间件
向中间件添加选项
$router->group('admin') // ... ->middleware( ActionLogMiddleware::class, options: [ 'methods' => ['POST', 'DELETE'], 'enabled' => (bool) env('ACTION_LOG_ENABLE'), 'max_time' => '7days', // ... ] ) // ...
准备日志处理器
添加一个自定义处理器来配置每个日志,必须使用 $log
参数来注入日志项。
以下是一个重写用户名的示例。
use Lyrasoft\ActionLog\Entity\ActionLog; use Lyrasoft\Luna\User\UserService; $router->group('admin') // ... ->middleware( ActionLogMiddleware::class, options: [ 'prepare_log' => function (ActionLog $log, UserService $userService) { $user = $userService->getUser(); $log->setName($user->getFirstName() . ' ' . $user->getLastName()); } ] ) // ...
如果您想忽略某些操作,只需在准备日志处理程序中返回 FALSE。
'prepare_log' => function (ActionLog $log, UserService $userService) { // ... if ($log->getTask() === '...') { // This log will not save return false; } }
手动记录日志
只需注入 ActionLogService
来完成此操作。
/** @var \Lyrasoft\ActionLog\Service\ActionLogService $actionLogService */ $actionLogService = $app->retrieve(\Lyrasoft\ActionLog\Service\ActionLogService::class); $appRequest = $app->retrieve(\Windwalker\Core\Http\AppRequest::class); $actionLogService->clearExpiredIfTriggered(); $actionLogService->log($appRequest, $response ?? null); // Or just create entity item. $log = $actionLogService->createLogItem($appRequest, $response ?? null);
自定义任务(操作)和实体渲染
默认情况下,后台列表表通过英文程序性名称显示任务和实体。
您可以通过事件自定义渲染名称。创建一个订阅者
namespace App\Subscriber; use Lyrasoft\ActionLog\Event\FormatEntityEvent; use Lyrasoft\ActionLog\Event\FormatTaskEvent; use Windwalker\Event\Attributes\EventSubscriber; use Windwalker\Event\Attributes\ListenTo; #[EventSubscriber] class ActionLogSubscriber { #[ListenTo(FormatTaskEvent::class)] public function formatTask(FormatTaskEvent $event): void { $text = &$event->getTaskText(); $log = $event->getLog(); $task = $log->getTask(); // Custom you task text // Same examples if ($task === 'relativeContracts') { $text = '相關合約操作'; } if ($task === 'relativeRentals') { $text = '相關委託操作'; } if ($task === 'addToCart') { $text = '加入購物車'; } } #[ListenTo(FormatEntityEvent::class)] public function formatEntity(FormatEntityEvent $event): void { $text = &$event->getEntityText(); $log = $event->getLog(); // Custom you entity text } }
将此订阅者注册到 etc/app/main.php
// ... 'listeners' => [ \Lyrasoft\ActionLog\Service\ActionLogService::class => [ \App\Subscriber\ActionLogSubscriber::class ] ],
事件中返回的文本将显示在表格列表中