定制 adithwidhiantara/laravel-async-audit-logger 二次开发

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

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

adithwidhiantara/laravel-async-audit-logger

Composer 安装命令:

composer require adithwidhiantara/laravel-async-audit-logger

包简介

High performance async audit logger

README 文档

README

codecov

High-Performance, Zero-Latency Audit Trail for Enterprise Laravel Applications.

This package provides an asynchronous audit logging system using Redis as a buffer and a Daemon Worker for bulk database insertion. It is designed to handle high-traffic applications where traditional synchronous logging would cause performance bottlenecks.

🚀 Why use this?

Traditional audit loggers insert a row into the database synchronously during the HTTP request.

  • Problem: If you have 1,000 requests/sec, you are hitting the DB with 2,000 queries/sec (1 transaction + 1 log). This slows down response time and risks locking the table.
  • Solution: This package pushes logs to Redis (memory) in <1ms. A background worker then processes them in batches (e.g., 100 logs at once). The user gets an instant response, and the database load is reduced by up to 90%.

📋 Requirements

  • PHP ^8.1
  • Laravel 10.x, 11.x, or 12.x
  • Redis Server (Required for Production)
  • PHP Extensions: pcntl (for worker signals), redis

📦 Installation

  1. Install via Composer:

    composer require adithwidhiantara/laravel-async-audit-logger
  2. Publish Configuration & Migrations:

    php artisan vendor:publish --provider="Adithwidhiantara\Audit\AuditingServiceProvider"
  3. Run Migrations:

    php artisan migrate
  4. Environment Setup (.env): Add these configurations to your .env file:

    # Driver: 'redis' (Production) or 'sync' (Local/Debugging)
    AUDIT_DRIVER=redis
    
    # Redis Connection (Recommend using a separate DB index, e.g., 'audit')
    AUDIT_REDIS_CONNECTION=default
    AUDIT_REDIS_KEY=audit_pkg:buffer
    
    # Worker Tuning
    AUDIT_BATCH_SIZE=100
    AUDIT_FLUSH_INTERVAL=5
    AUDIT_PRUNE_DAYS=90

🛠 Usage

1. Attach to Models

Simply use the Auditable trait on any Eloquent model you want to track.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Adithwidhiantara\Audit\Traits\Auditable; // <--- Import

class Product extends Model
{
    use Auditable; // <--- Use Trait

    protected $fillable = ['name', 'price', 'stock'];
}

That's it! Every created, updated, and deleted event will now be captured automatically.

2. Retrieving Logs

The trait provides a polymorphic relationship audits.

$product = Product::find(1);

// Get all history for this product
foreach ($product->audits as $audit) {
    echo $audit->event; // 'updated'
    echo $audit->user->name; // 'John Doe'
    print_r($audit->old_values); // ['price' => 1000]
    print_r($audit->new_values); // ['price' => 1200]
}

⚙️ Operational Setup (Production)

Since this is an asynchronous system, you must run the background processes.

1. The Worker (Daemon)

This worker consumes logs from Redis and saves them to the Database. Use Supervisor to keep it running.

Supervisor Config (/etc/supervisor/conf.d/audit-worker.conf):

[program:audit-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/app/artisan audit:work
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/app/storage/logs/audit-worker.log
stopwaitsecs=60

Tip: Increase numprocs if your traffic is extremely high.

2. The Scheduler (Recovery & Pruning)

Add these commands to your app/Console/Kernel.php (or routes/console.php in Laravel 11+).

  • audit:recover: Checks for failed logs (saved to JSON files during DB outages) and retries inserting them.
  • audit:prune: Automatically deletes old logs (default: >90 days).
// routes/console.php or Kernel.php

use Illuminate\Support\Facades\Schedule;

// Recover failed logs every hour
Schedule::command('audit:recover')->hourly()->runInBackground();

// Delete old logs daily
Schedule::command('audit:prune')->daily()->runInBackground();

⚖️ Pros & Cons

Feature Async (Redis) Sync (Direct DB)
User Latency Zero (<1ms) Standard (Waiting for DB)
Throughput High (Bulk Insert) Low (Row-by-row)
Reliability Fail-to-Disk (Rescue JSON) Fail = Error/Exception
Complexity High (Needs Supervisor) Low (Plug & Play)
Real-time Delayed (max 5s) Instant

🛡 Disaster Recovery

If the Database goes down:

  1. The Worker catches the exception.
  2. Data is saved to storage/logs/audit_rescue_*.json.
  3. The Worker continues processing new logs.
  4. Once the DB is up, the scheduled audit:recover command restores the data from the JSON files.

🤝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

📄 License

MIT

adithwidhiantara/laravel-async-audit-logger 适用场景与选型建议

adithwidhiantara/laravel-async-audit-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 adithwidhiantara/laravel-async-audit-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adithwidhiantara/laravel-async-audit-logger 我们能提供哪些服务?
定制开发 / 二次开发

基于 adithwidhiantara/laravel-async-audit-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-14