faustvik/yii2-audit-log

Audit log package for Yii2 with framework-agnostic core

Maintainers

Package info

github.com/FaustVik/yii2-audit-log

pkg:composer/faustvik/yii2-audit-log

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.2 2026-04-08 05:02 UTC

This package is auto-updated.

Last update: 2026-04-08 05:03:40 UTC


README

Audit logging package for Yii2 applications with framework-agnostic core.

Features

  • Automatic logging - INSERT, UPDATE, DELETE operations via Behavior
  • Events - Before/After log events for customization
  • Query builder - Advanced filtering with AuditLogQuery
  • Ready-to-use widgets - Display change history with filters
  • Framework-agnostic core - Can be used with any PHP framework
  • Yii2 integration - Behavior, Widgets, Migrations out of the box

Requirements

  • PHP 8.2+
  • Yii2 2.0.51+

Quick Start

1. Install via Composer

composer require faustvik/yii2-audit-log

2. Add Behavior to Model

use FaustVik\AuditLog\Yii2\Integration\AuditLogBehavior;

class User extends ActiveRecord
{
    public function behaviors(): array
    {
        return [
            'auditLog' => [
                'class' => AuditLogBehavior::class,
                'excludeAttributes' => ['hash_password', 'auth_key'],
            ],
        ];
    }
}

3. Display in View

<?= \FaustVik\AuditLog\Yii2\Integration\AuditLogFilterWidget::widget([
    'model' => $user,
    'limit' => 50,
    'title' => 'Change History',
]) ?>

Documentation

Structure

src/
├── Core/                    # Framework-agnostic core
│   ├── Contracts/           # Interfaces
│   ├── DTO/                 # Data Transfer Objects
│   ├── Enums/               # Enumerations
│   ├── Events/              # BeforeLogEvent, AfterLogEvent
│   ├── Exceptions/          # Exceptions
│   ├── Query/               # AuditLogQuery
│   └── Services/            # AuditLogger
│
└── Yii2/                    # Yii2 integration
    ├── Adapter/             # Adapters for Yii2
    ├── Integration/         # Behavior, Widgets
    └── Migrations/          # Migration base classes

Example Usage

Logging with Events

// In bootstrap.php
Yii::$app->on(
    \FaustVik\AuditLog\Core\Events\BeforeLogEvent::class,
    function (\FaustVik\AuditLog\Core\Events\BeforeLogEvent $event) {
        // Cancel logging for specific operations
        if ($event->entityClass === User::class && $event->operation->value === 'DELETE') {
            $event->stopPropagation();
        }
    }
);

Query Logs

use FaustVik\AuditLog\Core\Query\AuditLogQuery;
use FaustVik\AuditLog\Core\Contracts\AuditStorageInterface;

$storage = Yii::createObject(AuditStorageInterface::class);

$logs = (new AuditLogQuery($storage))
    ->forEntity(User::class, $userId)
    ->operation(\FaustVik\AuditLog\Core\Enums\Operation::Update)
    ->dateRange('2025-01-01', '2025-12-31')
    ->limit(50)
    ->all();

License

MIT

Authors

  • FaustVik