always-open/laravel-model-auditlog
Composer 安装命令:
composer require always-open/laravel-model-auditlog
包简介
Tracks changes made to models and logs them to individual tables.
README 文档
README
When modifying a model record, it is nice to have a log of the changes made and who made those changes. There are many packages around this already, but this one is different in that it logs those changes to individual tables for performance and supports real foreign keys.
Installation
You can install the package via composer:
composer require always-open/laravel-model-auditlog
Configuration
php artisan vendor:publish --provider="\AlwaysOpen\AuditLog\AuditLogServiceProvider"
Running the above command will publish the config file.
Usage
After adding the proper fields to your table, add the trait to your model.
// User model class User extends Model { use \AlwaysOpen\AuditLog\Traits\AuditLoggable;
To generate an auditlog model / migration for your models, use the following command:
php artisan make:model-auditlog "\\App\\User"
Replace \App\User with your own model name. Model, namespace, and table options can be tweaked in the config file.
If you need to ignore specific fields on your model, extend the getAuditLogIgnoredFields() method and return an array of fields.
public function getAuditLogIgnoredFields() : array { return ['posted_at']; }
Using this functionality, you can add more custom logic around what should be logged. An example might be to not log the title changes of a post if the post has not been published yet.
public function getAuditLogIgnoredFields() : array { if ($this->postHasBeenPublished()) { return ['title']; } return []; }
Working with Pivot Tables
Audit log can also support changes on pivot models as well.
In this example we have a posts and tags table with a post_tags pivot table containing a post_id and tag_id.
Modify the audit log migration replacing the subject_id column to use the two pivot columns.
Schema::create('post_tag_auditlog', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedInteger('post_id')->index(); $table->unsignedInteger('tag_id')->index(); $table->unsignedTinyInteger('event_type')->index(); $table->unsignedInteger('user_id')->nullable()->index(); $table->string('field_name')->index(); $table->text('field_value_old')->nullable(); $table->text('field_value_new')->nullable(); $table->timestamp('occurred_at')->index()->default('CURRENT_TIMESTAMP'); });
Create a model for the pivot table that extends Laravel's Pivot class. This class must use the AuditLoggablePivot trait and have a defined $audit_loggable_keys variable, which is used to map the pivot to the audit log table.
class PostTag extends Pivot { use AuditLoggablePivot; /** * The array keys are the composite key in the audit log * table while the pivot table columns are the values. * * @var array */ protected $audit_loggable_keys = [ 'post_id' => 'post_id', 'tag_id' => 'tag_id', ]; }
Side note: if a column shares the same name in the pivot and a column already in the audit log table (ex: user_id), change the name of the column in the audit log table (ex: audit_user_id) and define the relationship as 'audit_user_id' => 'user_id'.
The two models that are joined by the pivot will need to be updated so that events fire on the pivot model. Currently, a third-party package is required because Laravel doesn't support pivot model events.
composer require fico7489/laravel-pivot
Have both models use the PivotEventTrait
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait; use Illuminate\Database\Eloquent\Model; class Post extends Model { use PivotEventTrait;
Modify the belongsToMany join on both related models to include the using function along with the pivot model. In the Post model:
public function tags() { return $this->belongsToMany(Tag::class) ->using(PostTag::class); }
In the Tag model:
public function posts() { return $this->belongsToMany(Post::class) ->using(PostTag::class); }
When a pivot record is deleted through detach or sync, an audit log record for each of the keys (ex: post_id and tag_id) will be added to the audit log table. The field_value_old will be the id of the record and the field_value_new will be null. The records will have an event type of PIVOT_DELETED (id: 6).
If you need to pull the audit logs through the auditLogs relationship (ex: $post_tag->auditLogs()->get()), support for composite keys is required.
composer require awobaz/compoships
Then use the trait on the pivot audit log model:
use Awobaz\Compoships\Compoships; use AlwaysOpen\AuditLog\Models\BaseModel; class PostTagAuditLog extends BaseModel { use Compoships;
For a working example of pivots with the audit log, see laravel-model-auditlog/tests/Fakes, which contains working migrations and models.
Note:
Both models must use the AuditLoggable trait (ex: Post and Tag) so that $post->tags()->sync([...]) will work.
Testing
composer test
Using Docker
All assets are set up under the docker-compose.yml file. The first time you run the docker image you must build it with the following command:
docker-compose build
Then you can bring it up in the background using:
docker-compose up -d
And the image is aliased so you can access its command line via:
docker exec -it processes-stamp-app /bin/bash
From there you can run the tests within an isolated environment
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email @tomschlick / @qschmick instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
always-open/laravel-model-auditlog 适用场景与选型建议
always-open/laravel-model-auditlog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65.41k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 06 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「laravel」 「auditlog」 「always-open」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 always-open/laravel-model-auditlog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 always-open/laravel-model-auditlog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 always-open/laravel-model-auditlog 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Audit Plugin for CakePHP
A Zend Framework module that sets up Monolog for logging in applications.
Logging Plugin for CakePHP
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
统计信息
- 总下载量: 65.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-15