msonowal/laravel-auditor

一个简单的Mongo活动日志器,用于记录您的Laravel应用程序的各种事件

v1.2.0 2022-07-19 05:31 UTC

This package is auto-updated.

Last update: 2024-09-25 22:58:37 UTC


README

Latest Stable Version Total Downloads Daily Downloads License StyleCI

一个简单的包,用于跟踪、记录和记录Laravel应用程序事件和Eloquent模型的多态关系的变化。默认情况下,该包将所有审计活动存储在Mongo DB中的audit_logs集合中。但是,您可以通过配置来自定义所有内容。此包使用jenssegers/mongodb与Mongo DB交互。默认情况下,如果是由用户执行,则捕获每个请求的用户IP地址和用户代理。

安装

$ composer require msonowal/laravel-auditor

该包将自动注册自身。

配置

您可以选择使用以下内容发布配置文件

php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="config"

这是发布配置文件的内容

[

    /*
     * If set to false, no activities will be saved to the database.
     */
    'enabled' => env('AUDIT_ENABLED', true),

    /*
     * By default all the activities will be processed via queue
     * If set to false, all the activities will be processed instantly.
     */
    'use_queue' => env('AUDIT_MODE', true),

    /*
     * When the clean-command is executed, all recording activities older than
     * the number of days specified here will be deleted.
     */
    'delete_records_older_than_days' => 365,

    /*
     * When the clean-command is executed, all recording activities older than
     * the number of days specified above and if its beyond the max entries limit
     * those records will be deleted and mostly on the specified log name
     */
    'max_entries' => 50000,

    /*
     * If no log name is passed to the audit() helper
     * we use this default log name.
     */
    'default_log_name' => env('AUDIT_DEFAULT_LOG_NAME', 'default'),

    /*
     * If set to true, the subject returns soft deleted models.
     */
    'subject_returns_soft_deleted_models' => true,

    /*
     * This is the name of the database connection that will be used by the migration and
     * used by the Services.
     */
    'connection_name' => env('AUDIT_CONNECTION', 'activity'),
    /*
     * This is the name of the collection that will be created by the migration and
     * used by the AuditActivity model.
     */
    'collection_name' => env('AUDIT_COLLECTION_NAME', 'activity_logs'),
];

您可以使用以下内容发布迁移

php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="migrations"

注意:默认迁移将索引添加到集合的基本字段,但是您可以修改并定制以满足您的需求。

发布迁移后,您可以通过运行迁移来更新audit_logs集合上的索引

php artisan migrate

以下是如何使用它的示例

audit()->log('Something, has been done');

您可以使用AuditServiceRepository类检索所有活动。

在您的方法中注入

function test(AuditServiceRepository $audit)
{
    $audit->all(); //this returns all the records in plain array directly from DB

    $audit->paginate(); //this returns all the records as a Model Instance with default 50 per page and all the fields
}

以下是一个更高级的示例

audit()
   ->performedOn($anEloquentModel)
   ->causedBy($user)
   ->withProperties(['customProperty' => 'customValue'])
   ->add('Something, has been done');
   
$lastLoggedActivity = AuditActivityMoloquent::all()->last();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->getExtraProperty('customProperty'); //returns 'customValue'
$lastLoggedActivity->description; //returns 'Look, I logged something'

版本

根据Composer文档,版本

我们将遵循X.Y.Z或vX.Y.Z的格式,后跟一个可选的-suffix,例如-dev、-patch (-p)、-alpha (-a)、-beta (-b)或-RC。补丁、alpha、beta和RC后缀也可以跟一个数字。示例

  • 1.0.0
  • 1.0.2
  • 0.1.0
  • 0.2.5
  • 1.0.0-dev
  • 1.0.0-alpha3
  • 1.0.0-beta2
  • 1.0.0-RC5
  • v2.0.4-p1

安装依赖项后,您可以通过执行以下命令来运行所有测试

$ vendor/bin/phpunit

输出应类似于以下内容

.                                                                  1 / 1 (100%)

Time: 84 ms, Memory: 12.00MB

OK (1 test, 1 assertion)

所有测试文件都应位于tests/目录中。以下是一个示例

<?php

namespace Msonowal\Audit\Tests\Unit;

use Msonowal\Audit\Tests\TestCase;

class ExampleTest extends TestCase
{
    /** @test */
    public function example_test_method()
    {
        $this->assertTrue(true);
    }
}

待办事项

通过配置使模型事件可排队 使其更具可配置性 添加测试

致谢

此包受到spatie/activitylog的启发,这是一个用于在Laravel中记录MySQL或Laravel支持的数据库中的活动的包。

祝您玩得开心!🎊