oneseven9955/laravel-auditing-filesystem

该文件系统驱动程序是为 owen-it/laravel-auditing 包设计的,它便于将审核记录存储在所有已注册的 Laravel 磁盘的 CSV 文件中。

0.1.0 2024-06-07 21:01 UTC

This package is auto-updated.

Last update: 2024-09-30 17:36:26 UTC


README

此驱动程序提供将模型审核记录保存为 CSV 文件行记录的功能。它与 Laravel 存储系统无缝集成,使您可以使用应用程序中指定的任何已注册磁盘作为审核文件的存储目的地。

此外,该驱动程序在生成审核文件方面提供了灵活性,允许您选择创建一个综合的文件,或者为每个运营小时生成文件。此外,通过缓冲日志记录并在完成模型更改后一次性刷新,驱动程序可以潜在地提高性能。

安装

要使用此驱动程序,您需要安装 owen-it/laravel-auditing: ^13.0。一旦满足此要求,您可以按照以下步骤安装驱动程序

composer require OneSeven9955/laravel-auditing-fs

设置

如果您想修改驱动程序的默认行为,您必须在 config/audit.php 中包含以下配置条目。配置文件中的 drivers 键应该按照以下结构组织

    // ...
    'drivers' => [
        'database' => [
            'table'      => 'audits',
            'connection' => null,
        ],
        'filesystem' => [
            'disk'      => 'local',     // The registered name of any filesystem disk in the application
            'dir'       => 'audit',     // The directory on the disk where the audit csv files will be saved
            'filename'  => 'audit.csv', // The filename of the audit file
            'rotation'  => 'single',    // One of 'single', 'daily', or 'hourly'
        ],
    ],
    // ...

用法

您可以通过以下代码片段将驱动程序集成到任何可审计的模型中

<?php
namespace App\Models;

use OneSeven9955\Auditing\Drivers\FilesystemDriver;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;

class Article extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable;

    /**
     * Filesystem Audit Driver.
     *
     * @var \OneSeven9955\Auditing\Drivers\FilesystemDriver
     */
    protected $auditDriver = FilesystemDriver::class;

    // ...
}

为了优化写入审核记录的过程,请考虑缓冲记录并批量写入,而不是单独写入。这种方法有助于减少 I/O 操作,例如获取排他性文件锁和重复打开文件。

您可以通过以下步骤实现此优化

use OneSeven9955\Auditing\Drivers\FilesystemDriver;

app(FilesystemDriver::class)->bufferStart();
    // ...
    // PERFORM MODEL CHANGES
    // ...
app(FilesystemDriver::class)->bufferFlush(); // flush all audit records into a file at once