定制 tofiq/laravel-audit-trail 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tofiq/laravel-audit-trail

最新稳定版本:v0.1.0

Composer 安装命令:

composer require tofiq/laravel-audit-trail

包简介

README 文档

README

Laravel Audit Trail is a package that logs all SQL queries executed by your Laravel application, whether they originate from Eloquent Models or the Query Builder.

This package allows you to track query history effortlessly, requiring no modifications to your project beyond the initial setup. Once configured, all database interactions are automatically logged.

Installation

To install the package, run the following command via Composer.

composer require tofiq/laravel-audit-trail

After installation, publish the package configuration using:

php artisan vendor:publish --provider=Tofiq\\AuditTrail\\AuditTrailServiceProvider

This will generate a configuration file (config/audit-trail.php), where you can customize the package settings, also the necessary migration.

Then if you want to use the database trailer then run this as well:

php artisan migrate

Customizations

Using a Custom Eloquent Model

By default, Laravel Audit Trail stores logs in a database table. If you prefer to use a custom Eloquent model for audit logs, define it in the audit-trail.php configuration file:

<?php
return [
    ...

    'audit_model' => \App\Models\YourAuditTrailModel::class,
    ...
];

Ensure your custom model and migration are properly created and migrated.

Implementing a Custom Logging Service

If you need full control over how queries are logged (e.g., logging to a file, an external service, or a custom database structure), you can implement your own logging service.

  1. Create a custom service class implementing the AuditLoggerInterface:
<?php

namespace App\Services;

use Tofiq\AuditTrail\Contracts\AuditLoggerInterface;

class MyLogService implements AuditLoggerInterface
{
    public function log(string|null $tableName, string $operationType, string $query, array $bindings = [], int|float $time = 0): void
    {
        \Log::info('Query Logged', [
            'table' => $tableName,
            'operation' => $operationType,
            'query' => $query,
            'bindings' => $bindings,
            'execution_time' => $time,
        ]);
    }
}
  1. Register the custom service in a service provider:
    public function boot(): void
    {
        $this->app->bind(
            \Tofiq\AuditTrail\Contracts\AuditLoggerInterface::class,
            \App\Services\MyLogService::class
        );
    }

With this setup, Laravel Audit Trail will now use your custom logging service instead of the default database logger.

License

Laravel Audit Trail is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-02