lyrasoft/action-log 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lyrasoft/action-log

Composer 安装命令:

composer require lyrasoft/action-log

包简介

LYRASOFT Action Log Package

README 文档

README

Action Log

Installation

Install from composer

composer require lyrasoft/action-log

Then copy files to project

php windwalker pkg:install lyrasoft/action-log -t routes -t migrations

And run migrations.

Next, add Middleware to routes/admin.route.php

use Lyrasoft\ActionLog\Middleware\ActionLogMiddleware;

// ...

$router->group('admin')
    // ...
    ->middleware(ActionLogMiddleware::class)
    // ...

If you want to log front and admin, you may put middleware to main.route.php

Languages

Add this line to admin & front middleware to load language from package automatically:

use Lyrasoft\ActionLog\ActionLogPackage;

// ...

    $this->lang->loadAllFromVendor(ActionLogPackage::class, 'ini');

If you want to copy language file to project, run this:

php windwalker pkg:install lyrasoft/action-log -t lang

Register Admin Menu

Edit resources/menu/admin/sidemenu.menu.php

// Action Log
$menu->link('操作記錄')
    ->to($nav->to('action_log_list'))
    ->icon('fal fa-shoe-prints');

Getting Started

If you have setup ActionLog package, now try to save any item or filter any list, you can see a new log shows on action_logs table:

log

Auto Clear

Bt default, ActionLog only reserve last 3 months logs. It has 1/100 chance to trigger clear action.

You can configure the reserve time and clear chance at config file or using env to configure them:

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)
        ],

        // ...
    ]
];

Configure Middleware

Add options to middleware:

$router->group('admin')
    // ...
    ->middleware(
        ActionLogMiddleware::class,
        methods: ['POST', 'DELETE'],
        enabled: (bool) env('ACTION_LOG_ENABLE'),
        maxTime: '7days',
        // ...
    )
    // ...
Config Name Type Default Description
methods array, string, null ['POST', 'PUT', 'PATCH', 'DELETE'] Allowed method, use NULL to allow all
enabled bool true Enabled log, you may add your env to configure this.
max_time string, null null Max reserve time, default using config and env, can be datetime string.
clear_chance int, null null Auto clear chance, default using config and env.
clear_chance_base int, null null Auto clear chance base, default using config and env.
prepare_log callable, null null The handler to handle log item, can be callable or closure, can inject services.

Prepare Log Handler

Add a custom handler to configure every log, must use $log argument to inject log item.

This is an example to override user name.

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->name = $user->firstName . ' ' . $user->lastName;
            }
        ]
    )
    // ...

If you want to ignore some actions, just return FALSE in prepare log handler:

    'prepare_log' => function (ActionLog $log, UserService $userService) {
        // ...
        
        if ($log->getTask() === '...') {
            // This log will not save
            return false;
        }
    }

Manually Log

Just inject ActionLogService to do this.

/** @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);

Hide Sensitive Data

Add this to config file to hide some sensitive data from log:

// ...

    'hidden_list' => [
        'body' => [
            'password',
            'secret',
            // Add more fields...
        ],
    ]

View

Pagination

By default, ActionLog will display total pages:

Image

However, if you have a large number of logs, counting total rows may impact performance. You can disable total count by setting ACTION_LOG_COUNT_PAGES=0in the .env or .env.base files.

This will display a simple pagination without total pages:

Image

Limit Per-Page

Use ACTION_LOG_DISPLAY_LIMIT=xx in .env or .env.base to configure the number of items displayed per page. Default is 100.

Custom Task (Action) and Entity Display

By default, the admin list table shows task and entity by english programaticlly name.

You can custom the render name by events. Create a subscriber:

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
    {
        $log = $event->log;
        $task = $log->task;

        // Custom you task text
        
        // Same examples
        if ($task === 'relativeContracts') {
            $event->taskText = '相關合約操作';
        }

        if ($task === 'relativeRentals') {
            $event->taskText = '相關委託操作';
        }

        if ($task === 'addToCart') {
            $event->taskText = '加入購物車';
        }
    }

    #[ListenTo(FormatEntityEvent::class)]
    public function formatEntity(FormatEntityEvent $event): void
    {
        $log = $event->getLog();

        // Custom you entity text
        $event->entityText = '...';
    }
}

Register this subscriber to etc/app/main.config.php:

// ...

        'listeners' => [
            \Lyrasoft\ActionLog\Service\ActionLogService::class => [
                \App\Subscriber\ActionLogSubscriber::class
            ]
        ],

The text you return from event will show at table list:

tasks

lyrasoft/action-log 适用场景与选型建议

lyrasoft/action-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.56k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lyrasoft/action-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lyrasoft/action-log 我们能提供哪些服务?
定制开发 / 二次开发

基于 lyrasoft/action-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-15