PHPackages                             aifst/laravel-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. aifst/laravel-logger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

aifst/laravel-logger
====================

Logger package for Laravel 5.6 and up

1.0.11(2w ago)097MITPHPPHP ^7.4|^8.0

Since Jul 18Pushed 2w ago1 watchersCompare

[ Source](https://github.com/aifst/laravel-logger)[ Packagist](https://packagist.org/packages/aifst/laravel-logger)[ Docs](https://github.com/aifst/laravel-logger)[ RSS](/packages/aifst-laravel-logger/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (8)Dependencies (6)Versions (13)Used By (0)

Laravel Logger
==============

[](#laravel-logger)

A lightweight audit log for Eloquent models. Add one trait and every create / update / delete is recorded — with the acting user, a before/after diff, and an optional owner (the scope the event happened in, e.g. a project or a site).

Each entry stores:

- **subject** — the model the action was performed on (`model_type` / `model_id`)
- **action** — `created` / `updated` / `deleted`
- **user** — who caused it (`user_id`)
- **before / after** — the changed attributes (a diff for updates, the full record for create/delete)
- **owner** *(optional)* — the polymorphic scope container the event happened in (`owner_type` / `owner_id`), or null for a global entry
- **comment** *(optional)* — a free-text reason for the action, attached by the application through the `LogSaving` event

Installation
------------

[](#installation)

Install via Composer:

```
composer require "aifst/laravel-logger:^1.0.0"
```

The service provider is auto-discovered. To register it manually, add it to `config/app.php`:

```
'providers' => [
    // ...
    Aifst\Logger\LoggerServiceProvider::class,
];
```

Publish the migration and `config/logger.php`, then migrate:

```
php artisan vendor:publish --provider="Aifst\Logger\LoggerServiceProvider"
php artisan migrate
```

Usage
-----

[](#usage)

Add the `Logger` trait to any model you want audited:

```
use Aifst\Logger\Traits\Logger;

class Course extends Model
{
    use Logger;
}
```

By default all three events are logged. Opt out per model by overriding the flags:

```
protected static function loggedCreated(): bool  { return true; }
protected static function loggedUpdating(): bool { return true; }
protected static function loggedDeleting(): bool { return false; }
```

### Recording the acting user

[](#recording-the-acting-user)

The trait does not know your auth layer, so tell it how to resolve the current user id (return `null` for system/unauthenticated actions):

```
protected static function loggerUserId()
{
    return auth()->id();
}
```

### Limiting the logged attributes

[](#limiting-the-logged-attributes)

By default the whole record (minus `id` / timestamps) is captured. Restrict it to specific columns:

```
protected static function loggedFields(): ?array
{
    return ['title', 'status'];
}
```

### Owner scope (optional)

[](#owner-scope-optional)

A log entry records the **subject** it happened to. You may also record an **owner** — the scope container the event happened *in* (a project, a site, a tenant…). The owner is polymorphic and nullable, so entries can be scoped to a project, to a site, or left global, without tying the log to any single entity — and it is distinct from the subject.

Override `loggerOwner()` to return the owning model:

```
protected function loggerOwner()
{
    return $this->project; // or Site::current(), or null for a global entry
}
```

The entry then stores `owner_type` / `owner_id` (honouring a registered morph map).

### Reason for the action (`comment`) via the `LogSaving` event

[](#reason-for-the-action-comment-via-the-logsaving-event)

*Why* something was deleted or changed cannot be derived from a diff — only the calling code knows it. Every entry is therefore dispatched as `Aifst\Logger\Events\LogSaving` after it is built and **before** it is saved, so a listener can enrich it in place:

```
use Aifst\Logger\Events\LogSaving;

Event::listen(function (LogSaving $event) {
    // $event->log — the unsaved entry, $event->model — the subject, $event->action — created/updated/deleted
    $event->log->comment = DeletionReason::current(); // e.g. a request-scoped reason
});
```

A common pattern is a request-scoped holder: the service that deletes a record sets the reason it received from the user, the listener copies it onto the entry, and the holder is cleared afterwards. Anything the listener writes to `$event->log` is persisted with the entry.

Querying
--------

[](#querying)

The `Log` model exposes read helpers:

```
use Aifst\Logger\Models\Log;

// A model's own history (via the Logger trait's relation)
$course->logs()->latest()->get();

// By owner scope (all activity in a project)
Log::forOwner($project)->latest()->get();
Log::forOwner($project->getMorphClass(), $project->id)->get();

// By action
Log::wasCreated()->get();
Log::wasUpdated()->get();
Log::wasDeleted()->get();

// By subject entity
Log::entity($course->getMorphClass(), $course->id)->get();

// In a time window
Log::between($from, $to)->get();

// Reconstruct a model's state at a point in time
$snapshot = $course->logs()->stateOn($datetime);
```

`before` and `after` are returned as arrays (JSON is decoded automatically).

Upgrading an existing install (owner scope)
-------------------------------------------

[](#upgrading-an-existing-install-owner-scope)

If your `logs` table predates the owner columns, add them with a migration:

```
$table->string('owner_type')->nullable();
$table->unsignedBigInteger('owner_id')->nullable();
$table->index(['owner_type', 'owner_id']);
```

Existing rows keep a null owner; nothing else changes.

Upgrading an existing install (comment)
---------------------------------------

[](#upgrading-an-existing-install-comment)

If your `logs` table predates the `comment` column, add it with a migration:

```
$table->string('comment', 255)->nullable();
```

Existing rows keep a null comment; the `LogSaving` event fires regardless of the column, so add the column before any listener starts writing to it.

License
-------

[](#license)

MIT.

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance96

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~134 days

Recently: every ~10 days

Total

12

Last Release

20d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cb89f6055d40fbeff99b25f5dea7fd659dad353cc526d0cfd7d7135b7eedb09a?d=identicon)[yspkerk](/maintainers/yspkerk)

---

Top Contributors

[![aifst](https://avatars.githubusercontent.com/u/49309374?v=4)](https://github.com/aifst "aifst (12 commits)")[![spyluk](https://avatars.githubusercontent.com/u/38014819?v=4)](https://github.com/spyluk "spyluk (4 commits)")

---

Tags

laravelloggeraifst

### Embed Badge

![Health badge](/badges/aifst-laravel-logger/health.svg)

```
[![Health](https://phpackages.com/badges/aifst-laravel-logger/health.svg)](https://phpackages.com/packages/aifst-laravel-logger)
```

###  Alternatives

[laravel/cashier

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.

2.6k31.8M160](/packages/laravel-cashier)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k16.3M154](/packages/laravel-pulse)[spatie/laravel-health

Monitor the health of a Laravel application

88212.7M188](/packages/spatie-laravel-health)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[mollie/laravel-cashier-mollie

Laravel Cashier provides an expressive, fluent interface to Mollie's subscription billing services.

179223.2k1](/packages/mollie-laravel-cashier-mollie)[masterro/laravel-mail-viewer

Easily view in browser outgoing emails.

64115.4k](/packages/masterro-laravel-mail-viewer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
