yiier / yii2-humans-log

人类能看得懂的操作日志

安装次数: 347

依赖者: 0

建议者: 0

安全性: 0

星标: 6

关注者: 5

分支: 1

开放问题: 0

类型:yii2-extension

v1.3.1 2020-02-14 14:08 UTC

This package is auto-updated.

Last update: 2024-09-14 23:58:06 UTC


README

人类能看得懂的操作日志

Latest Stable Version Total Downloads Latest Unstable Version License

描述

  • 此扩展只要你按照约定的规则,可以帮你记录操作日志。
  • 只能监控单条数据,所以不适用于需要操作多条数据。
  • 无法做到颗粒度很细的日志,比方说你要记录谁操作了订单的状态,此扩展是无法做到的,你只能记录谁操作了订单,也只能记录订单的最新状态,操作之前的状态无法记录。
  • 有特殊情况的话,可以使用 yiier\humansLog\models\HLog::saveLog() 方法单独记录日志。
  • 模板使用说明可以看截图,也可以看 src\views\h-log-template\_form.php 文件。

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

php composer.phar require --prefer-dist yiier/yii2-humans-log "*"

或者

"yiier/yii2-humans-log": "*"

将以下内容添加到你的 composer.json 文件的 require 部分中。

迁移

运行以下命令

php yii migrate --migrationPath=@yiier/humansLog/migrations/

配置

一旦扩展安装完成,只需按照以下方式修改你的应用程序配置

return [
    'modules' => [
        'humans-log' => [
            'class' => 'yiier\humansLog\Module',
            // 'mainLayout' => '@app/views/layout/hlog.php',
            // 'safeDelete' => false
        ],
    ],
];

用法

方法一(推荐)

你需要在 bootstrap 部分中包含它

return [
    'bootstrap' => [
        'yiier\humansLog\EventBootstrap',
    ],
];

方法二

按照以下方式配置控制器类

use use yiier\humansLog\HLogBehavior;

class Controller extends \yii\web\Controller
{
    public function behaviors()
    {
        return [
            HLogBehavior::className(),
        ];
    }
}

按照以下方式配置模型类

use use yiier\humansLog\HLogBehavior;

class ActiveRecord extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            HLogBehavior::className(),
        ];
    }
}

DIY 使用

示例

模板

INSERT INTO `h_log_template` (`id`, `title`, `unique_id`, `template`, `method`, `status`, `created_at`, `updated_at`)
VALUES
	(16, '订单备注记录', 'orderRemarkUpdateRecord', '订单 {order_number} 的备注从『{old-remark}』更新为 『{remark}』', 5, 1, 1561953330, 1561953330);
public function afterSave($insert, $changedAttributes)
{
    parent::afterSave($insert, $changedAttributes);
    if (!\Yii::$app instanceof \yii\console\Application) {
        $changedRemark = ArrayHelper::getValue($changedAttributes, 'remark');
        if (!$insert && $this->remark != $changedRemark) {
            $logData = [
                'order_number' => $this->order_number,
                'old-remark' => $changedRemark,
                'remark' => $this->remark
            ];
            HLog::saveLog('orderRemarkUpdateRecord', $logData, $this->order_number);
        }
    }
}

路由

然后你可以通过以下 URL 访问 Merit 模块

https:///path/to/index.php?r=humans-log/h-log
https:///path/to/index.php?r=humans-log/h-log-template
https:///path/to/index.php?r=humans-log/h-log-template/create

截图

Create log Template

Log Template

Logs