herca/activity-log

Activity logging package for Laravel

Maintainers

Package info

github.com/metcore/activity-log

pkg:composer/herca/activity-log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-04-17 08:25 UTC

This package is not auto-updated.

Last update: 2026-04-21 05:48:23 UTC


README

A lightweight Laravel package for recording model activity logs, both automatically and manually.

Features

  • Automatic logging for created, updated, and deleted model events
  • Manual logging through a fluent ActivityLogger service
  • Polymorphic relation between the logged model and activity entries
  • Snapshot support for attributes, changes, and old values
  • Request correlation through request_id
  • Optional actor tracking through created_by
  • Configurable ignored attributes

Installation

Install the package with Composer in your Laravel application:

composer require herca/activity-log

Register the service provider if your Laravel version does not support package auto-discovery.

Publish Assets

Publish the configuration:

php artisan vendor:publish --tag=activity-log-config

Publish the migration:

php artisan vendor:publish --tag=activity-log-migrations

Run migrations:

php artisan migrate

Basic Usage

Add the LogActivity trait to any Eloquent model you want to track.

<?php

namespace App\Models;

use Herca\ActivityLog\Traits\LogActivity;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use LogActivity;
}

Once attached, the model will automatically record activity for:

  • created
  • updated
  • deleted

Manual Logging

You can also create manual activity entries using the helper:

activity_log($post)
    ->causedBy(auth()->user())
    ->withNote('Post published manually')
    ->withSnapshot([
        'attributes' => $post->getAttributes(),
        'changes' => ['status' => 'published'],
        'old' => ['status' => 'draft'],
    ])
    ->log('published');

If your model uses the provided contract/trait API, you can also log directly from the model:

$post->logActivity('published', [
    'note' => 'Published from admin panel',
    'changes' => ['status' => 'published'],
    'old' => ['status' => 'draft'],
]);

Controlling Logging

You can enable or disable logging at runtime:

$post->disableLogging();
$post->enableLogging();
$post->setLogActivityEnabled(false);

You can also manage ignored attributes:

$post->setLogIgnoreAttributes(['updated_at', 'last_seen_at']);
$post->addLogIgnoreAttribute('synced_at');
$post->removeLogIgnoreAttribute('synced_at');
$post->clearRuntimeLogIgnoreAttributes();

Activity Model

The package stores logs with fields such as:

  • type
  • snapshot
  • note
  • request_id
  • related_id
  • created_by
  • created_at

Each activity entry belongs to a subject through a polymorphic relation.

Request ID Resolution

The package resolves request_id in this order:

  1. get_request_id() helper when enabled
  2. Request headers such as X-Request-Id or X-Correlation-Id
  3. Request attribute request_id
  4. A generated ULID or ordered UUID

Events

An ActivityLogged event is dispatched whenever a new activity record is stored. You can listen to this event to trigger notifications, auditing workflows, or downstream processing.

Notes

  • Update the Composer package name in the installation example to match your published package.
  • Review the config file after publishing to customize table name, model class, and ignored attributes.

License

This package is open-sourced under the MIT license.