myzero1/

yii2-log

可以通过文本、截图或两者同时进行日志记录。

安装: 51

依赖: 0

建议: 0

安全: 0

星级: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-module

1.1.4 2019-01-06 14:14 UTC

This package is not auto-updated.

Last update: 2024-09-21 15:09:28 UTC


README

日志模块,包括文本日志、截图日志以及两者都包含。

显示时间

安装

安装此模块的首选方式是通过 composer

运行以下命令

php composer.phar require myzero1/yii2-log:1.*

"myzero1/yii2-log": "*"

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

设置

扩展安装完成后,只需修改您的应用程序配置(main.php)如下

return [
    ......
    'bootstrap' => [
        ......
        'z1log',
        ......
    ],
    'modules' => [
        ......
        'z1log' => [
            'class' => '\myzero1\log\Module',    
            'params' => [
                'urlManager' => [
                    'rules' => [
                        // 'rate/area/index' => 'rate/jf-core-area/index',
                    ],
                ],
                'remarksFieldsKey' => [
                    'remark', // default filed
                    // 'r1', // custom field, can add it by yourself
                ],
                'userInfo' => [
                    'id' => function(){
                        if(\Yii::$app->user->isGuest){
                            $id = 0;
                        } else {
                            $id = \Yii::$app->user->identity->id;
                        }

                        return $id;
                    },
                    'name' => function(){
                        if(\Yii::$app->user->isGuest){
                            $name = 'system';
                        } else {
                            $name = \Yii::$app->user->identity->username;
                        }
                        
                        return $name;
                    }
                ],
                'template' => [
                    'user2/create' => [
                        'model' => 'all', // text,screenshot,all
                        'addToTable' => 'user', // for creating
                        'text' => function(){
                            return '添加用户'; 
                        },
                        'screenshot' => 'user2/update', // The template of screenshot
                        'obj' => [
                            'label' => '.field-user2-username .control-label',
                            'value' => '#user2-username',
                        ],
                        'remarks' => [// the items must be Closure
                            'remark' => function(){
                                return sprintf('Create it at %s.', date('Y-m-d H:i:s'));
                            },
                            // 'r1' => function(){return 'r1'.time();},
                        ],
                    ],
                    'user2/update' => [
                        'model' => 'all', // text,screenshot,all
                        'text' => function(){
                            return '修改用户'; 
                        },
                        'screenshot' => 'user2/update', // The template of screenshot
                        'obj' => [
                            'label' => '.field-user2-username .control-label',
                            'value' => '#user2-username',
                        ],
                        'remarks' => [// the items must be Closure
                            'remark' => function(){
                                return sprintf('Update it at %s.', date('Y-m-d H:i:s'));
                            },
                        ],
                    ],
                    'user2/delete' => [
                        'model' => 'all', // text,screenshot,all
                        'text' => function(){
                            return '删除用户'; 
                        },
                        'screenshot' => 'user2/update', // The template of screenshot
                        'obj' => [
                            'label' => '.field-user2-username .control-label',
                            'value' => '#user2-username',
                        ],
                        'remarks' => [// the items must be Closure
                            'remark' => function(){
                                return sprintf('Delete it at %s.', date('Y-m-d H:i:s'));
                            },
                        ],
                    ],
                ],
            ],
        ],
        ......
    ],
    ......
];

应用迁移

    php yii migrate --migrationPath=@vendor/myzero1/yii2-log/src/migrations

使用方法

您可以通过以下 URL 访问 Demo

http://localhost/path/to/index.php?r=z1log/z1log-log/index

或者如果您已启用漂亮 URL,您可以使用以下 URL

http://localhost/path/to/index.php/z1log/z1log-log/index

在任何地方使用 z1logAdd($model, $screenshot, $screenshotParams, $text, $obj, $remarks)

\myzero1\log\components\export\Export::z1logAdd('all', 'user2/update', ['id'=>$model->id], 'create user', sprintf('username:%s', $model->username), ['remark'=>'this is a remark']);

使用场景

  • 只需将配置添加到 mian.php,当我们想要添加日志时,会有相应的动作更新。· 截图将记录更新前的数据。

  • 当我们要创建新记录时,使用 z1logAdd api,在动作中我们得到新记录的 id,非常简单。

    /**
     * Creates a new User2 model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User2();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            
            \myzero1\log\components\export\Export::z1logAdd('all', 'user2/update', ['id'=>$model->id], 'create user', sprintf('username:%s', $model->username), '');

            Yii::$app->getSession()->setFlash('success', '添加成功');
            return \myzero1\adminlteiframe\helpers\Tool::redirectParent(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
  • 当我们要添加日志,但没有更新动作时,使用 z1logAdd api。