ramaid/laravel-auditor

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

dev-master 2023-08-28 02:45 UTC

This package is auto-updated.

Last update: 2024-09-28 05:23:17 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的格式,后跟可选的-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支持的数据库中日志活动的包。

祝您玩得开心! 🎊