devlab-studio / laravel-logs
Package to manage logs in Laravel applications.
Fund package maintenance!
Requires
- php: ^8.3
- illuminate/contracts: ^13.0|^12.0
- laravel/jetstream: ^5.4
- laravel/sanctum: ^4.0
- laravel/tinker: ^3.0|^2.0
- livewire/livewire: ^4.0
- spatie/laravel-package-tools: ^1.16
- spatie/laravel-permission: ^6.7
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
README
Laravel Logs is a Laravel package for advanced logs and audits management, allowing you to register, query, and clean logs efficiently in your Laravel projects.
- Register and query custom logs
- Audit models and procedures
- Commands to clean and manage logs
- Flexible and extensible configuration
What It Does
This package adds a lightweight audit layer for Eloquent models by logging save and delete operations.
- Tracks procedure names such as
App\\Models\\Order::saveandApp\\Models\\Order::delete - Stores change payloads in JSON format
- Records the user responsible for the action
- Keeps a normalized table of procedures for easier filtering/reporting
Installation
Install the package via Composer:
composer require devlab-studio/laravel-logs
Publish and run the migrations:
php artisan vendor:publish --tag=laravel-logs-migrations php artisan migrate
Publish the configuration file:
php artisan vendor:publish --tag=laravel-logs-config
Useful Commands
Clean old logs:
php artisan logs:clear
logs:clear removes records older than 12 months.
How It Works
- Your model extends the package base model (
Devlab\\LaravelLogs\\Models\\Model). - On
save(), the package detects dirty attributes and logs original vs changed data. - On
delete(), the package logs the deleted model ID. - The procedure name is registered in
models_proceduresif it does not exist yet. - The action is stored in
models_logswith timestamps andcreated_user.
Database Structure
models_procedures
idprocedure(example:App\\Models\\Invoice::save)created_at,updated_at
models_logs
idprocedureprocedure_id(FK tomodels_procedures.id)data(JSON / text)created_usercreated_at,updated_at
Usage Example
Extend your models from the package base model:
<?php namespace App\Models; use Devlab\LaravelLogs\Models\Model; class Customer extends Model { protected $fillable = ['name', 'email']; }
Create and update records as usual:
$customer = Customer::create([ 'name' => 'Jane Doe', 'email' => 'jane@example.com', ]); $customer->email = 'jane.doe@example.com'; $customer->save();
Each operation writes an audit entry in models_logs.
Querying Logs
You can query logs directly with Eloquent:
use Devlab\LaravelLogs\Models\ModelsLog; $latestLogs = ModelsLog::query() ->latest('id') ->limit(20) ->get();
The package also includes helper methods in ModelsLog (like dlGet) for filtered retrieval.
Seeders
If you need sample data for testing, you can create your own seeders to populate the logs table.
Notes
- The package expects an authenticated user for
created_userwhen available. - If no authenticated user exists, it falls back to
config('constants.users.system'). - Ensure your application defines that fallback config value for CLI/background contexts.