承接 stealthfirems/laravel-audit-module 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

stealthfirems/laravel-audit-module

Composer 安装命令:

composer require stealthfirems/laravel-audit-module

包简介

Auditor module for Laravel applications.

README 文档

README

A module to provide the ability and flexibility to easily audit events and actions that happen within your Laravel application. Designed around StealthFireMS Laravel applications

Installation

install the package via composer:

composer require stealthfirems/laravel-audit-module

run this

php artisan vendor:publish --provider="StealthFireMS\LaravelAuditModule\Providers\AuditModuleServiceProvider" --tag="AuditModule"

Once you've done this, run your migrations. This will create a table called audit_logs.

php artisan migrate

Configuration

Variables for your root.env file

Base template

AUDIT_MODULE_RETENTION=30
AUDIT_MODULE_OBFUSCATE_IP=false
AUDIT_MODULE_TABLE_NAME=audit_logs # table name

Obfuscating the IP address for compliance

You can easily obfuscate IP addresses that are submitted to the database by setting the AUDIT_MODULE_OBFUSCATE_IP variable in your .env file to true. This will strip the first two octets of an IP address, ensuring it meets various compliance laws, such as GDPR. Behind the scenes this switches the default IP address fetcher with an Obfuscated IP fetcher.

Setting the retention duration

You can define how many days your logs should be kept for by setting the AUDIT_MODULE_RETENTION in your .env file. If you want to keep all logs indefinitely, set this to 0.

Every time the audit logs are pruned, this will be recorded as an audit log itself.

Usage

There are multiple ways you can use this package. The most common way is to use the audit helper function.

Using the global audit helper

The audit helper function is an easy way to quickly log to the audits table. This function takes a string as the first argument, and an optional array (context) as the second argument. This will only work if you don't already have a global function called audit.

Example 1:

audit('login', [
    'severity' => 'info',
]);

Example 2:

audit('logout', [
    'severity' => 'info',
    'context'=> '$FailedEmailAddress',
]);

Helper Break Down:

audit('Action be performed', [
    'severity' => 'serverity of the action being logged',
    'context' => 'Extra content being stored in the log'
]);

Table Break down

$table->bigIncrements('id'); // Primary key for the log entry

$table->string('action')->index(); //Action being performed in the log

$table->string('severity')->index(); // Severity level of the log | default is 'info'

$table->longText('context')->nullable()->default(null); // Contextual information about the action

$table->string('type')->; //User or system action | System by default unless user is specified

$table->unsignedBigInteger('user_id')->index()->nullable(); // ID of the user performing the action

$table->string('guard')->nullable(); // Guard used for authentication

$table->string('ip_address'); // IP address of the user performing the action

$table->timestamp('created_at')->useCurrent(); // Timestamp of when the log was created

Binding to events

If you want to audit an event that happens within your application, you can do so by using the IsAuditableEvent interface. Coupled with AuditableEvent, this will automatically log the event to the audit log.

Here's an example of an event that utilises the IsAuditableEvent interface:

// import our contract & trait
use StealthFireMS\LaravelAuditModule\Contracts\IsAuditableEvent;
use StealthFireMS\LaravelAuditModule\Traits\AuditableEvent;

class MyCustomEvent implements IsAuditableEvent
{
    use AuditableEvent;

    public function handle()
    {
        // ToDo: your event logic.
    }
    
    // optional - by default will be handled by the AuditableEvent trait
    public function getAuditMessage(): string
    {
        return 'Action performed';
    }
    
    // optional - by default will be handled by the AuditableEvent trait
    public function getAuditContext(): array
    {
        return ['more_data' => 'Goes here'];
    }
}

Using the AuditableModel trait on Models

If you have a model that you'd like to be audited on change, you can use the AuditableModel trait. By default, this will record all creations, updates and deletions for this model to the audit log. This uses Laravel model observers to listen for changes. By default, the created_at and updated_at columns are excluded from auditing.

use Illuminate\Database\Eloquent\Model;
use StealthFireMS\LaravelAuditModule\Traits\AuditableModel;

class YourModel extends Model
{
    use AuditableModel;
    
    /**
    * An array of columns that shouldn't be audited.
    * @var array|string[] 
    */
    protected array $excludedFromAuditing = [
        'created_at',
        'updated_at',
    ];
}

Customising the AuditableModel functionality

If you'd like to expand the functionality of the AuditableModel trait, you can override its observer by configuring the observer key in the config file. This will allow you to create your own model observer.

use StealthFireMS\LaravelAuditModule\Observers\AuditableModelObserver as BaseObserver;

class AuditableObserver extends BaseObserver
{
    // your custom classes here
    // see https://laravel.com/docs/11.x/eloquent#observers for more information
}

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues use the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

stealthfirems/laravel-audit-module 适用场景与选型建议

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

它主要适用于以下技术方向: 「Audit」 「laravel」 「stealthfirems」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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