Add the Activity Log package

Merged
BI
3h ago
+14325-18

This adds hypervel/activity-log, an optional package for recording application activity in a database. An entry can describe an action, the model it affected, who performed it, and any properties the application wants to keep.

activity('billing')
    ->performedOn($invoice)
    ->causedBy($user)
    ->event('paid')
    ->log('Invoice paid');

Models can also use LogsActivity to record Eloquent events. The model chooses which attributes to log and whether to keep only changes. By default, the logger reads the model as it was at the event. Applications that need database-generated values can opt into a refresh inside the writing transaction.

Persistence

Activities are saved immediately by default. Applications can opt into buffering to insert rows in batches. Each coroutine owns its buffer and its current causer. For an activity recorded on a connection with an open transaction, a rollback discards the buffered entry. After commit, a later batch writes it. Applications that need the activity and the domain change to commit together can use immediate writes on the same connection.

Installation and use

Composer discovers the service provider. The application publishes the migration before running it, so it can choose the appropriate morph keys and indexes. The package includes a cleanup command with configurable retention and optional scheduling. The guide covers installation, querying, model events, transaction behavior, and differences from Spatie’s Laravel Activity Log.

Verification

The package tests cover model changes, causer resolution, coroutine isolation, buffer flushing, and rollback. The migration and row-locking tests ran against MySQL, MariaDB, and PostgreSQL. A clean application installed the split package, published and ran the migration, and recorded an activity through the helper.

[!NOTE]

Add the hypervel/activity-log package

  • Introduces a full activity-log package: an Activity Eloquent model with query scopes (inLog, causedBy, forSubject, forEvent), the LogsActivity/CausesActivity/HasActivity model concerns, a fluent ActivityLogger, an activity() helper, and the ActivityLog facade in ActivityLogManager.php
  • Includes optional coroutine-scoped buffering via ActivityBuffer.php: activities staged inside a transaction are discarded on rollback and flushed on commit, app termination, or job completion
  • Adds the activity-log:clean console command and CleanActivityLogAction.php, which deletes old activities in bounded batches, plus a publishable migration and configuration in activity-log.php
  • Supports custom activity models, tables, and database connections, with causer resolution from auth, IDs, models, or callbacks in CauserResolver.php
  • Behavioral Change: composer.json now conflicts with spatie/laravel-activitylog, so the two packages cannot be installed together. Also adds small Eloquent changes: SoftDeletes.restore() tracks an isBeingRestored() flag, MorphTo placeholders skip result resolution and touching, and Model.performInsert/performInsertOrIgnore now share getDatabaseAttributesForInsert() in HasAttributes.php

Macroscope summarized 68ab5cf.