pfrug/clear-logs

Smart cleanup of Laravel logs by file or content date.

v1.1.0 2025-05-27 14:28 UTC

This package is auto-updated.

Last update: 2025-05-27 14:43:50 UTC


README

Artisan command to clean up Laravel log files, with support for both single and daily log channels. It preserves logs from the last N days, configurable by content date or file modification time.

Features

  • Works with single and daily log channels
  • Retains logs by configurable number of days
  • Detects dates from log content, not just filenames
  • Supports --dry-run mode (preview changes)
  • Optional file name or mtime-based cleanup for daily logs
  • Clean truncation of large single log files by full log blocks

Installation

Install the package via Composer:

composer require pfrug/clear-logs

Optional: Publish configuration

php artisan vendor:publish --provider="Pfrug\ClearLogs\ClearLogsServiceProvider" --tag="clear-logs-config"

This will publish config/clearlogs.php.

Configuration file options

return [

    // Number of days to preserve (default: 7)
    'days' => 7,

    // Mode used to determine the file date for daily logs
    // Options: 'name' or 'mtime'
    'evalDateByNameOrMTime' => 'name',
];

Usage

Run the command manually:

php artisan log:clear

Or preview the result without modifying files:

php artisan log:clear --dry-run

Override the number of days on the fly:

php artisan log:clear --days=10

Scheduler Integration

To run daily, register the command in your scheduled tasks (app/Console/Kernel.php):

protected function schedule(Schedule $schedule): void
{
    // Run daily at 2 AM
    $schedule->command('log:clear')->dailyAt('02:00');
}

Supported Channels

  • single: The default laravel.log file is parsed and truncated. Only complete log blocks newer than the cutoff are retained.
  • daily: Individual laravel-YYYY-MM-DD.log files are deleted if older than the configured threshold.

License

MIT © P. Frugone