joshbrw / laravel-audit-logging
在 Laravel 中实现完全可翻译的审计日志。
v0.1.2
2017-08-30 11:43 UTC
Requires
- php: ^7.0
- alsofronie/eloquent-uuid: ^1.0
- laravel/framework: ^5.4|^5.5
Requires (Dev)
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ^6.2
This package is auto-updated.
Last update: 2024-08-28 23:44:57 UTC
README
这是一个简单易用的审计日志系统,具有中心化的翻译功能。使用 UUID 进行数据集合并和管理。
安装
- 在
config/app.php
的providers
键下添加Joshbrw\AuditLogging\AuditLoggingServiceProviders,
。 - 在
config/app.php
的aliases
键下添加'Audit' => Joshbrw\AuditLogging\Facades\Audit::class,
以注册 Facade - 运行
php artisan vendor:publish --provider="Joshbrw\\AuditLogging\\AuditLoggingServiceProvider"
- 这会将迁移(s)发布到您的顶级database/migrations
目录。 - 运行
php artisan migrate
以运行此包提供的迁移(s)。 - 在您希望记录日志的每个 Eloquent 模型中,添加
Joshbrw\AuditLogging\Traits\Eloquent\Auditable
特性。
用法
基本属性
每个审计日志项都有两个必需的属性
auditable_type
|auditable_id
- 审计相关的实体message_key
- 消息的翻译键
可选属性包括
message_replacements
- 应传递给翻译器的替换数组,用于显示审计日志。data
- 任何您想存储在审计日志中的数组/对象数据。user_id
- 执行被记录操作的用户 ID。 默认支持 Laravel 的 Auth 和 Sentinel,允许通过 UserResolver 使用自定义适配器
使用辅助方法和服务
此库提供了一个 audit()
方法,语法如下
/** * Helper method for logging an audit entry. * @param mixed $entity The entity to create the log for * @param string $messageKey The key of the translatable message to use * @param array|null $messageReplacements Array of replacements for the message * @param array|null $data Any data to attribute to the audit log item * @return AuditLog Audit log instance */ function audit($entity, string $messageKey, array $messageReplacements = null, array $data = null): AuditLog {
它简单地将代理到 Joshbrw\AuditLogging\AuditLogManager
服务器的 log()
方法,该方法接受相同的参数。
外观
此包还提供了一个外观,可以像 \Audit::
一样使用,并代理到 AuditLogManager
,例如 Audit::log()
和 Audit::translate()
。
获取审计日志
您可以使用 AuditLogRepository
获取特定 Eloquent 模型的审计日志
- 确保实体使用
Auditable
特性。 - 查看以下代码了解如何按逆时间顺序获取模型的全部审计日志
$user = App\User::first(); $auditLogs = app(Joshbrw\AuditLogging\Repositories\AuditLogRepository::class)->getAllAuditLogs($user);