msonowal/laravel-auditor
Composer 安装命令:
composer require msonowal/laravel-auditor
包简介
A simple mongo activity logger to record various events of your laravel application
README 文档
README
A simple package to track, record and log changes of your laravel apps events and also
Eloquent Models by Polymorphic relations. By Default, the Package stores all audit activity
in the audit_logs collection in the Mongo DB. However you can customize everything via config.
This package uses jenssegers/mongodb for interacting with Mongo DB.
By Default Users Ip address and User Agents are Captured for every request if it is performed by Users.
Installation
$ composer require msonowal/laravel-auditor
The package will automatically register itself.
Configuration
You can optionally publish the config file with:
php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="config"
This is the contents of the published config file:
[
/*
* If set to false, no activities will be saved to the database.
*/
'enabled' => env('AUDIT_ENABLED', true),
/*
* By default all the activities will be processed via queue
* If set to false, all the activities will be processed instantly.
*/
'use_queue' => env('AUDIT_MODE', true),
/*
* When the clean-command is executed, all recording activities older than
* the number of days specified here will be deleted.
*/
'delete_records_older_than_days' => 365,
/*
* When the clean-command is executed, all recording activities older than
* the number of days specified above and if its beyond the max entries limit
* those records will be deleted and mostly on the specified log name
*/
'max_entries' => 50000,
/*
* If no log name is passed to the audit() helper
* we use this default log name.
*/
'default_log_name' => env('AUDIT_DEFAULT_LOG_NAME', 'default'),
/*
* If set to true, the subject returns soft deleted models.
*/
'subject_returns_soft_deleted_models' => true,
/*
* This is the name of the database connection that will be used by the migration and
* used by the Services.
*/
'connection_name' => env('AUDIT_CONNECTION', 'activity'),
/*
* This is the name of the collection that will be created by the migration and
* used by the AuditActivity model.
*/
'collection_name' => env('AUDIT_COLLECTION_NAME', 'activity_logs'),
];
You can publish the migration with:
php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="migrations"
Note: The default migration adds the indexes to the collection for the essentials fields however you can modify and tailor upto your needs.
After publishing the migration you can update the indexes on the audit_logs collection by running the migrations:
php artisan migrate
Here's a demo of how you can use it:
audit()->log('Something, has been done');
You can retrieve all activity using the AuditServiceRepository class.
Inject in your methods
function test(AuditServiceRepository $audit) { $audit->all(); //this returns all the records in plain array directly from DB $audit->paginate(); //this returns all the records as a Model Instance with default 50 per page and all the fields }
Here's a more advanced example:
audit() ->performedOn($anEloquentModel) ->causedBy($user) ->withProperties(['customProperty' => 'customValue']) ->add('Something, has been done'); $lastLoggedActivity = AuditActivityMoloquent::all()->last(); $lastLoggedActivity->subject; //returns an instance of an eloquent model $lastLoggedActivity->causer; //returns an instance of your user model $lastLoggedActivity->getExtraProperty('customProperty'); //returns 'customValue' $lastLoggedActivity->description; //returns 'Look, I logged something'
Version
According to the composer docs the version:
We will follow the format of X.Y.Z or vX.Y.Z with an optional suffix of -dev, -patch (-p), -alpha (-a), -beta (-b) or -RC. The patch, alpha, beta and RC suffixes can also be followed by a number. Examples:
- 1.0.0
- 1.0.2
- 0.1.0
- 0.2.5
- 1.0.0-dev
- 1.0.0-alpha3
- 1.0.0-beta2
- 1.0.0-RC5
- v2.0.4-p1
Testing After install the dependencies you can run all the tests by excecuting the follow command:
$ vendor/bin/phpunit
The output should look similar to this:
. 1 / 1 (100%)
Time: 84 ms, Memory: 12.00MB
OK (1 test, 1 assertion)
All the test files should be inside the tests/ directory. Here is an example:
<?php namespace Msonowal\Audit\Tests\Unit; use Msonowal\Audit\Tests\TestCase; class ExampleTest extends TestCase { /** @test */ public function example_test_method() { $this->assertTrue(true); } }
TODO:
Make the model event queable via config Make It more configurable Add Tests
Credits
This package was inspired by their work on spatie/activitylog a package to use log activity in laravel in mysql or supported db by laravel.
Have fun! 🎊
msonowal/laravel-auditor 适用场景与选型建议
msonowal/laravel-auditor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.31k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2019 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「user」 「mongo」 「Audit」 「laravel」 「activity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 msonowal/laravel-auditor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 msonowal/laravel-auditor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 msonowal/laravel-auditor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
Pacote simplificado para persistência de logs
MongoDB extension for the Yii framework
Collection of tools to use the full power of the Enyalius framework
Standalone replacement for php's native get_browser() function
统计信息
- 总下载量: 32.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-04