Search by

jiordiviera / laravel-log-cleaner

jiordiviera

A Laravel package to easily clean the log files.

Package info

github.com/jiordiviera/laravel-log-cleaner

pkg:composer/jiordiviera/laravel-log-cleaner

Statistics

Installs: 3 723

Dependents: 0

Suggesters: 0

Stars: 10

Open Issues: 7

v2.4.0 2026-11-18 01:45 UTC

This package is auto-updated.

Last update: 2026-11-23 16:10:51 UTC


README

Laravel Log Cleaner — simple, safe log management for Laravel

Clean up Laravel logs safely, without the clutter.

Packagist version Downloads Tests MIT license

Laravel Log Cleaner is a lightweight Laravel package that removes old log entries using an Artisan command. Preview changes first, keep backups, compress removed entries and handle large files using streaming.

Installation

composer require jiordiviera/laravel-log-cleaner

Requirements: PHP 8.2+ and Laravel 10–13. Laravel 13 requires PHP 8.3+. Actual compatibility also depends on the framework's PHP requirements. See Packagist for older releases.

Quick start

# Remove all logs
php artisan log:clear

# Remove entries older than 30 days
php artisan log:clear --days=30

# Preview changes without modifying files
php artisan log:clear --days=30 --dry-run

# Create a backup before cleaning
php artisan log:clear --days=30 --backup

Features

  • Safe cleanup: dry-run, cooperative file locking, backups and checks before replacing files.
  • Targeted retention: choose age, log level, custom date pattern or a specific file.
  • Compression: save removed entries in a gzip archive.
  • Large files: stream data to reduce peak memory use.
  • Health report: log:stats shows size, records per level and date range, and can fail above a size limit.
  • Scheduling: optional automatic log:clear through Laravel's scheduler.
  • Integrations: Laravel facade, configurable defaults and lifecycle events.

Why and how it is built

Deleting logs by hand on a production server is tedious and easy to get wrong: you can remove entries you still need, or replace a file while the application is writing to it. This package turns that chore into a repeatable command, built around a few deliberate choices:

  • One core, several entry points. The cleaning logic lives in a single service. The Artisan command, the scheduler and the facade all call it, so a cleanup behaves the same however it is triggered.
  • Streaming above a memory threshold. Files larger than memory_threshold (50 MB by default), or any file with --memory-efficient, are read line by line, so memory use stays flat whatever the log size. Smaller files take the simpler in-memory path.
  • Never edit a log in place. Kept entries are written to a temporary file next to the original, the original is checked for changes, then the temporary file replaces it with rename(). With --dry-run and --backup, you can see and keep everything before anything is removed.
  • Cooperative locking. Each file gets a non-blocking flock() lock, so two cleanups (for example the scheduler and a manual run) never process the same file at once. The trade-off: the lock does not block the application's own log writers (see Important limitations).
  • Events instead of built-in side effects. LogCleaning, LogFileCleaned and LogCleaned let you add alerts, audit trails or metrics without changing the package.

Command options

Option Purpose
--days=30 Remove entries older than 30 days
--dry-run Preview cleanup without writing
--backup Back up a log before modifying it
--compress Archive removed entries as gzip
--level=ERROR Filter by log level
--pattern=REGEX Use a custom date pattern
--file=laravel.log Select one log file
--memory-efficient Force streaming mode
--no-lock Disable cooperative cleanup locking
--no-events Disable cleanup events

For example:

php artisan log:clear --days=7 --backup --compress
php artisan log:clear --days=30 --file=laravel.log --dry-run
php artisan log:clear --days=0 --level=ERROR

Log health report

log:stats is read-only: it streams each log file and reports its size, records per level, and the oldest and newest record dates, plus the backups and archives left by earlier cleanups.

php artisan log:stats
php artisan log:stats --file=laravel.log
php artisan log:stats --json
php artisan log:stats --max-size=500   # exit code 1 above 500 MB, for monitoring

Stack trace lines are not counted as records. The size limit can also be set with health.max_size_mb (or LOG_CLEANER_MAX_SIZE_MB).

Scheduled cleaning

The package can register log:clear in Laravel's scheduler for you. Enable it in config/log-cleaner.php (or with LOG_CLEANER_SCHEDULE_ENABLED=true):

'schedule' => [
    'enabled' => true,
    'cron' => '0 2 * * *',      // every day at 02:00
    'timezone' => null,
    'options' => ['days' => 30, 'backup' => false, 'compress' => true],
],

Your server still needs Laravel's usual schedule:run cron entry (or php artisan schedule:work). Check the registration with php artisan schedule:list.

Programmatic usage

use JiordiViera\LaravelLogCleaner\LogCleanerFacade as LogCleaner;

LogCleaner::clearAll();
LogCleaner::clearOld(30);
LogCleaner::clearWithBackup(30);
LogCleaner::clearWithCompression(30);

LogCleaner::clear(
    days: 30,
    backup: true,
    compress: true,
    file: 'laravel.log'
);

Configuration

Publish the optional configuration file:

php artisan vendor:publish --tag="log-cleaner-config"

You can adjust the memory threshold, backup retention, compression level, default log level, file-lock timeout, event dispatching, scheduled cleaning and the health size limit in config/log-cleaner.php.

Events and exceptions

Listen to these events to observe cleanup:

  • LogCleaning — before cleanup starts.
  • LogFileCleaned — after a file has been cleaned.
  • LogCleaned — after cleanup completes.

The package also provides dedicated exceptions for invalid parameters, permissions, disk space, backups, locking and compression. See the classes in src/Exceptions.

Development

composer install
composer test
composer analyse
composer lint

Tests use Pest; formatting uses Laravel Pint; static analysis runs PHPStan level 4. The CI matrix covers PHP 8.2/8.3 with Laravel 10–12, plus PHP 8.3 with Laravel 13.

Important limitations

  • Concurrent writers: locks coordinate participating cleanup processes, not unrelated log writers. A write between the final integrity check and replacement may still race.
  • Multiline logs: continuation lines follow the last recognized log header. Unrecognized, undated content may be retained.
  • Disk space: streaming and backups still need free space for temporary files and archives.
  • Compression: requires PHP's zlib extension.

Releases and contributions

Changes merged into main are not necessarily published on Packagist. See the changelog for release history and unreleased changes.

Issues and pull requests are welcome in the GitHub repository.

Released under the MIT License.