joshbrw/laravel-audit-logging

在 Laravel 中实现完全可翻译的审计日志。

v0.1.2 2017-08-30 11:43 UTC

This package is auto-updated.

Last update: 2024-08-28 23:44:57 UTC


README

这是一个简单易用的审计日志系统,具有中心化的翻译功能。使用 UUID 进行数据集合并和管理。

安装

  1. config/app.phpproviders 键下添加 Joshbrw\AuditLogging\AuditLoggingServiceProviders,
  2. config/app.phpaliases 键下添加 'Audit' => Joshbrw\AuditLogging\Facades\Audit::class, 以注册 Facade
  3. 运行 php artisan vendor:publish --provider="Joshbrw\\AuditLogging\\AuditLoggingServiceProvider" - 这会将迁移(s)发布到您的顶级 database/migrations 目录。
  4. 运行 php artisan migrate 以运行此包提供的迁移(s)。
  5. 在您希望记录日志的每个 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 模型的审计日志

  1. 确保实体使用 Auditable 特性。
  2. 查看以下代码了解如何按逆时间顺序获取模型的全部审计日志
$user = App\User::first();
$auditLogs = app(Joshbrw\AuditLogging\Repositories\AuditLogRepository::class)->getAllAuditLogs($user);