PHPackages                             parvion/laravel-log-pruner - 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. [Database &amp; ORM](/categories/database)
4. /
5. parvion/laravel-log-pruner

ActiveLibrary[Database &amp; ORM](/categories/database)

parvion/laravel-log-pruner
==========================

A robust Laravel package for atomic log rotation, dynamic database table pruning, queue worker restart, and multi-recipient email reporting.

1.0.0(2mo ago)02MITPHPPHP ^8.0

Since May 25Pushed 2mo agoCompare

[ Source](https://github.com/AnandKumar2002/laravel-log-pruner)[ Packagist](https://packagist.org/packages/parvion/laravel-log-pruner)[ RSS](/packages/parvion-laravel-log-pruner/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (6)Versions (2)Used By (0)

Parvion Laravel Log Pruner
==========================

[](#parvion-laravel-log-pruner)

[![PHP Version](https://camo.githubusercontent.com/8374974966349d091d449f65bd658093c8c436ccd163d9df90b670a3a20359d6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e302d383839324246)](https://php.net)[![Laravel](https://camo.githubusercontent.com/e6c348de52be9b1322cbd21a1f88fc446194890e4a720fa01507a04ba4d38a42/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d31302532302537432532303131253230253743253230313225323025374325323031332d4646324432303f6c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465)](https://laravel.com)[![License: MIT](https://camo.githubusercontent.com/1e64768fef09f35b66921728160f533208fd2e3e792a2755187d16c25d535511/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d3232633535652e737667)](LICENSE)[![Changelog](https://camo.githubusercontent.com/b1e1d00cef4334604482247128c2158554fef6e952f0ac2e6bae488607f5d246/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6368616e67656c6f672d4348414e47454c4f472e6d642d306561356539)](CHANGELOG.md)[![Contributing](https://camo.githubusercontent.com/91418e41d9135648e8186cb30a8a24f712560231ff9177bed5673f08a6ad2334/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6e747269627574696e672d434f4e545249425554494e472e6d642d663539653062)](CONTRIBUTING.md)[![Artisan Command](https://camo.githubusercontent.com/cdd0638c838c9e23e27d1d9c7387c93c79565fe35b854947bc90f9dc61d9c903/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6172746973616e2d6c6f6773253341726f746174652d2d616e642d2d7072756e652d363336366631)](https://laravel.com/docs/artisan)

> A robust, enterprise-ready Laravel package for atomic log rotation, dynamic database table pruning, queue worker restart, and multi-recipient email reporting — all from one Artisan command, fully controlled from your `config/log-pruner.php` and `.env`.

---

Table of Contents
-----------------

[](#table-of-contents)

1. [Features](#features)
2. [How It Works — The 5 Phases](#how-it-works--the-5-phases)
3. [Requirements](#requirements)
4. [Installation](#installation)
5. [Configuration](#configuration)
6. [Usage](#usage)
7. [Scheduling](#scheduling)
8. [Email Report Example](#email-report-example)
9. [Backup Info Table Example](#backup-info-table-example)
10. [.env Quick Reference](#env-quick-reference)
11. [Disabling Features](#disabling-features)
12. [License](#license)

---

Features
--------

[](#features)

FeatureDetails🔄 **Atomic log rotation**Uses OS-level `rename()` — zero window where log is missing📋 **Backup info table**Shows every backup: size, age, expiry date, and SAFE/EXPIRED status🗑️ **Backup file pruning**Auto-deletes backups older than your configured retention period🔧 **Queue worker restart**Gracefully signals workers to release old file handles🗃️ **Dynamic DB pruning**Safely prunes any database table with schema existence checks📧 **Multi-recipient email**Plain-text report via `Mail::raw()` — works with ZeptoMail, Postmark, Mailgun🛡️ **Schema safety checks**Verifies table + `created_at` column exist before any DELETE⚙️ **Fully config-driven**Every feature toggled from `config/log-pruner.php` and `.env`🎛️ **CLI overrides config**Pass `--days`, `--tables`, `--email` to override config on the fly---

How It Works — The 5 Phases
---------------------------

[](#how-it-works--the-5-phases)

Each phase can be individually enabled or disabled in the config.

```
┌─────────────────────────────────────────────────────────────────┐
│                  php artisan logs:rotate-and-prune              │
├─────────────────────────────────────────────────────────────────┤
│  PHASE 1 — Log Rotation       (features.log_rotation)          │
│    rename(laravel.log → laravel-backup-2026-05-23-020000.log)  │
│    touch(laravel.log)  chmod(0664)                             │
├─────────────────────────────────────────────────────────────────┤
│  PHASE 2 — Backup Pruning     (features.backup_pruning)        │
│    Scan storage/logs/ for laravel-backup-*.log                 │
│    Print info table (name, size, age, expiry, status)          │
│    Delete files older than retention threshold                  │
├─────────────────────────────────────────────────────────────────┤
│  PHASE 3 — Queue Restart      (features.queue_restart)         │
│    Artisan::call('queue:restart')                              │
│    Workers gracefully restart → open fresh laravel.log         │
├─────────────────────────────────────────────────────────────────┤
│  PHASE 4 — Database Pruning   (features.db_pruning)            │
│    For each table → check exists → check created_at column     │
│    DELETE WHERE created_at < cutoff_date                        │
├─────────────────────────────────────────────────────────────────┤
│  PHASE 5 — Email Report       (features.email_report)          │
│    Validate each recipient → Mail::raw() → one email per addr  │
│    Report includes all phases + backup file table              │
└─────────────────────────────────────────────────────────────────┘

```

---

Requirements
------------

[](#requirements)

DependencyVersion**PHP**`^8.0` (8.0, 8.1, 8.2, 8.3+)**Laravel**`10.x`, `11.x`, `12.x`, `13.x`**Carbon**Included with Laravel---

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

[](#installation)

### Step 1 — Add the package

[](#step-1--add-the-package)

```
composer require parvion/laravel-log-pruner
```

> Laravel's auto-discovery registers the service provider automatically. No manual `config/app.php` edit required.

---

### Step 2 — Publish the config

[](#step-2--publish-the-config)

```
php artisan vendor:publish --tag=log-pruner-config
```

This copies the config to your app at `config/log-pruner.php`. You can now customise every setting without touching the package source.

---

### Step 3 — Set your `.env` values

[](#step-3--set-your-env-values)

Open your `.env` and add:

```
# How many days to keep backup files and DB rows
LOG_PRUNER_DAYS=15

# Tables to prune (comma-separated, supports custom days via table:days)
LOG_PRUNER_TABLES=system_logs:10,audit_logs

# Email recipients for the report (comma-separated)
LOG_PRUNER_MAIL_ENABLED=true
LOG_PRUNER_MAIL_RECIPIENTS=admin@yourdomain.com,devops@yourdomain.com
```

---

### Step 4 — Verify the command is registered

[](#step-4--verify-the-command-is-registered)

```
php artisan list | grep logs
# Expected output:
#  logs:rotate-and-prune   Atomically rotates laravel.log, prunes old backups...
```

---

### Step 5 — Run it manually to test

[](#step-5--run-it-manually-to-test)

```
php artisan logs:rotate-and-prune
```

---

Configuration
-------------

[](#configuration)

After publishing, edit `config/log-pruner.php`. Every key maps to a `.env` variable:

```
return [

    // ── Master on/off switch ──────────────────────────────────────────────
    'enabled' => env('LOG_PRUNER_ENABLED', true),

    // ── Retention period (days) ───────────────────────────────────────────
    'days' => env('LOG_PRUNER_DAYS', 15),

    // ── Enable / disable each phase independently ─────────────────────────
    'features' => [
        'log_rotation'   => env('LOG_PRUNER_FEATURE_ROTATION',      true),
        'backup_pruning' => env('LOG_PRUNER_FEATURE_BACKUP_PRUNING', true),
        'queue_restart'  => env('LOG_PRUNER_FEATURE_QUEUE_RESTART',  true),
        'db_pruning'     => env('LOG_PRUNER_FEATURE_DB_PRUNING',     true),
        'email_report'   => env('LOG_PRUNER_FEATURE_EMAIL',          true),
    ],

    // ── Tables to DELETE old rows from (with custom retention) ────────────
    'tables' => [
        'system_logs' => 10,   // Keeps system logs for 10 days
        'audit_logs',          // Keeps audit logs for global retention (15 days)
    ],

    // ── Backup file display settings ──────────────────────────────────────
    'backup' => [
        'show_info'          => env('LOG_PRUNER_BACKUP_SHOW_INFO',       true),
        'show_info_in_email' => env('LOG_PRUNER_BACKUP_SHOW_INFO_EMAIL', true),
        'date_format'        => env('LOG_PRUNER_BACKUP_DATE_FORMAT',     'Y-m-d H:i'),
    ],

    // ── Email report settings ─────────────────────────────────────────────
    'mail' => [
        'enabled'        => env('LOG_PRUNER_MAIL_ENABLED',        true),
        'recipients'     => array_filter(array_map('trim',
                              explode(',', env('LOG_PRUNER_MAIL_RECIPIENTS', '')))),
        'subject_prefix' => env('LOG_PRUNER_MAIL_SUBJECT_PREFIX', '[Log Pruner]'),
    ],
];
```

---

Usage
-----

[](#usage)

### Basic — uses all config defaults

[](#basic--uses-all-config-defaults)

```
php artisan logs:rotate-and-prune
```

### Override retention period

[](#override-retention-period)

```
# Keep 30 days instead of config default
php artisan logs:rotate-and-prune --days=30
```

### Override which tables to prune

[](#override-which-tables-to-prune)

```
# Prune with global retention days
php artisan logs:rotate-and-prune --tables=system_logs,audit_logs

# Prune with custom, table-specific retention days (table:days format)
php artisan logs:rotate-and-prune --tables=system_logs:10,audit_logs:30
```

### Override email recipients

[](#override-email-recipients)

```
# Send report to specific people for this run only
php artisan logs:rotate-and-prune --email=cto@example.com,sre@example.com
```

### Full override example

[](#full-override-example)

```
php artisan logs:rotate-and-prune \
  --days=7 \
  --tables=system_logs,audit_logs \
  --email=admin@example.com,devops@example.com
```

> **Priority:** CLI option → config/log-pruner.php → built-in default

---

Scheduling
----------

[](#scheduling)

### Laravel 11+ (`routes/console.php`)

[](#laravel-11-routesconsolephp)

```
