DEV Community

Cover image for Laravel Log Cleaner v2.01 - Memory-Efficient Log Management with Compression & Backup
Jiordi Viera
Jiordi Viera

Posted on

Laravel Log Cleaner v2.01 - Memory-Efficient Log Management with Compression & Backup

Laravel Log Cleaner v2.0 - Memory-Efficient Log Management

The Problem ๐Ÿ”ฅ

We've all been there: your Laravel app runs fine for weeks, then suddenly your server runs out of disk space. You SSH in, check the logs folder, and find a 5GB laravel.log file.

Sound familiar?

Log files can grow out of control fast, especially in production. But clearing them manually is risky, and heavy monitoring tools like Telescope can slow down your app or even crash your server.

The Solution โœจ

I built Laravel Log Cleaner to solve this exact problem. Version 2.0 just launched with major improvements:

  • ๐Ÿง  Memory-efficient processing - handles multi-GB files without crashing
  • ๐Ÿ“ฆ Compression support - archive old logs instead of deleting
  • ๐Ÿ”’ Backup creation - never lose important data
  • ๐ŸŽฏ Log level filtering - keep only ERROR logs, discard the rest
  • ๐Ÿ‘€ Dry-run mode - preview changes before applying
  • โšก 50%+ performance improvement on large files

Installation

composer require jiordiviera/laravel-log-cleaner
Enter fullscreen mode Exit fullscreen mode

That's it! No config files, no service providers to register. Laravel's auto-discovery handles everything.

Basic Usage

Clear all logs:

php artisan log:clear
Enter fullscreen mode Exit fullscreen mode

Keep the last 30 days:

php artisan log:clear --days=30
Enter fullscreen mode Exit fullscreen mode

Advanced Features (What's New in v2.0)

1. Safe Operations ๐Ÿ”’

Preview what will be deleted without actually deleting:

php artisan log:clear --days=30 --dry-run
Enter fullscreen mode Exit fullscreen mode

Output:

[DRY RUN] Would remove 15,420 lines from laravel.log
[DRY RUN] Estimated space to free: 45.2 MB
Enter fullscreen mode Exit fullscreen mode

Create a backup before cleaning:

php artisan log:clear --days=30 --backup
Enter fullscreen mode Exit fullscreen mode

Output:

Backup created: storage/logs/laravel.log.backup.2024-11-05-14-30-15
Logs older than 30 days have been removed.
Enter fullscreen mode Exit fullscreen mode

2. Compression Instead of Deletion ๐Ÿ“ฆ

Archive old logs for compliance or audit purposes:

php artisan log:clear --days=30 --compress
Enter fullscreen mode Exit fullscreen mode

This creates laravel.log.old.2024-11-05.gz - perfect for when you need to keep logs but save disk space.

3. Log Level Filtering ๐ŸŽฏ

Keep only critical errors, discard everything else:

php artisan log:clear --days=0 --level=ERROR
Enter fullscreen mode Exit fullscreen mode

This is huge for production. Most of your logs are probably DEBUG or INFO - useful during development but just noise after a few days in production.

Supported levels: DEBUG, INFO, WARNING, ERROR, CRITICAL

4. Memory-Efficient Processing ๐Ÿง 

The game-changer for huge log files:

php artisan log:clear --days=30 --memory-efficient
Enter fullscreen mode Exit fullscreen mode

Before v2.0: A 2GB log file would crash PHP with "out of memory" errors

After v2.0: Processes the same file using stream processing, never loads more than 50MB in RAM

The package automatically detects large files (>50MB) and enables memory-efficient mode, but you can force it with this flag.

5. Custom Date Patterns ๐Ÿ”

Got non-standard log formats? No problem:

php artisan log:clear --days=30 --pattern="/^(\d{4}-\d{2}-\d{2})/"
Enter fullscreen mode Exit fullscreen mode

6. Combine Everything ๐Ÿš€

The complete workflow:

php artisan log:clear --days=30 --backup --compress --level=ERROR --dry-run
Enter fullscreen mode Exit fullscreen mode

This command:

  1. Previews what would be deleted (dry-run)
  2. Creates a backup before making changes
  3. Compresses old logs instead of deleting
  4. Keeps only ERROR level logs

Real-World Use Case

Here's how I use it in production:

Laravel 11+ (Current)

Schedule in routes/console.php:

use Illuminate\Support\Facades\Schedule;

// Every day at 2 AM - keep 7 days of ERROR logs
Schedule::command('log:clear', [
    '--days' => 7,
    '--backup' => true,
    '--level' => 'ERROR'
])->daily()->at('02:00');

// Weekly deep clean with compression
Schedule::command('log:clear', [
    '--days' => 30,
    '--compress' => true
])->weekly()->sundays()->at('03:00');
Enter fullscreen mode Exit fullscreen mode

Laravel 10 and Below

Schedule in app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('log:clear', [
        '--days' => 7,
        '--backup' => true,
        '--level' => 'ERROR'
    ])->daily()->at('02:00');

    $schedule->command('log:clear', [
        '--days' => 30,
        '--compress' => true
    ])->weekly()->sundays()->at('03:00');
}
Enter fullscreen mode Exit fullscreen mode

Result:

  • โœ… Keeps 7 days of ERROR logs for quick debugging
  • โœ… Archives everything older in compressed format
  • โœ… Never runs out of disk space
  • โœ… No manual intervention needed
  • โœ… Backups created automatically

Performance Benchmarks ๐Ÿ“Š

I tested this on a real production log file from one of my client's projects:

Test File: 1.2 GB, 3.5 million lines

Operation v1.0 v2.0 Improvement
Clear all logs 45s 12s 73% faster โšก
Filter by date (30 days) Memory error โŒ 18s Now possible โœ…
Memory usage (peak) 512MB+ 48MB 90% reduction ๐ŸŽฏ
Compression Not supported 8s New feature ๐Ÿ†•

Why Not Use X? ๐Ÿค”

vs. Manual deletion (rm laravel.log or echo "" > laravel.log)

  • โŒ No selective date filtering
  • โŒ Risk of deleting current day's logs
  • โŒ No backup option
  • โŒ Can't filter by log level

vs. Laravel Telescope

  • โŒ Telescope is heavy (database overhead, UI, background jobs)
  • โŒ Can actually slow down your app in production
  • โœ… Laravel Log Cleaner has zero runtime overhead
  • โœ… Only runs when you schedule it

vs. Other log cleaner packages

Most alternatives:

  • โŒ Don't handle memory efficiently (crash on large files)
  • โŒ No compression support
  • โŒ No log level filtering
  • โŒ No dry-run mode for safety
  • โŒ Limited or no backup functionality

Compatibility

Current Version (v2.x):

  • PHP 8.1, 8.2, 8.3+
  • Laravel 9.x, 10.x, 11.x, 12.x

Legacy Support (v1.x):
If you're still on older versions:

composer require jiordiviera/laravel-log-cleaner:^1.0
Enter fullscreen mode Exit fullscreen mode
  • PHP 7.0+
  • Laravel 7.x, 8.x

Migration from v1.x

Upgrading from v1.x? It's seamless!

Step 1: Update via Composer

composer update jiordiviera/laravel-log-cleaner
Enter fullscreen mode Exit fullscreen mode

Step 2: Check PHP/Laravel requirements

  • Requires PHP 8.1+
  • Requires Laravel 9+

Step 3: Enjoy new features!
All your existing commands still work. The new features are opt-in via command flags.

Breaking changes:

  • Dropped PHP 7.x support
  • Dropped Laravel 7.x and 8.x support
  • If you need these versions, stay on v1.x

Common Use Cases

1. Development: Keep Recent Logs Only

php artisan log:clear --days=3
Enter fullscreen mode Exit fullscreen mode

2. Production: Aggressive Cleanup with Safety

php artisan log:clear --days=7 --backup --level=ERROR
Enter fullscreen mode Exit fullscreen mode

3. Compliance: Archive Everything

php artisan log:clear --days=90 --compress
Enter fullscreen mode Exit fullscreen mode

4. Emergency: Server Running Out of Space

# Preview impact first
php artisan log:clear --days=1 --dry-run

# If safe, execute
php artisan log:clear --days=1 --backup
Enter fullscreen mode Exit fullscreen mode

5. CI/CD Pipeline: Clean After Tests

php artisan log:clear --days=0
Enter fullscreen mode Exit fullscreen mode

What's Next? ๐Ÿ”ฎ

I'm considering these features for future releases:

  • ๐Ÿ—‚๏ธ Multiple log file support (--file=api.log)
  • ๐Ÿ“ง Slack/Email notifications on cleanup completion
  • โš™๏ธ Config file for environment-specific retention policies
  • โ˜๏ธ Cloud storage integration (S3, GCS, Azure)
  • ๐Ÿ“Š Log analytics before cleanup (show stats)
  • ๐Ÿ”„ Auto-rotation based on file size (not just days)

What would YOU like to see? Drop a comment below! Your feedback shapes the roadmap.

Try It Out

composer require jiordiviera/laravel-log-cleaner
php artisan log:clear --help
Enter fullscreen mode Exit fullscreen mode

Links:

Contributing

Found a bug? Have a feature request? Contributions are welcome!

  1. Fork the repo
  2. Create a feature branch
  3. Write tests (we use Pest)
  4. Submit a PR

Check out the Contributing Guide.


About Me

I'm Jiordi Viera, a fullstack developer from Douala, Cameroon ๐Ÿ‡จ๐Ÿ‡ฒ. I work primarily with Laravel, Next.js, and React.

I built this package after dealing with production log issues one too many times. What started as a simple script evolved into a full package when I realized others had the same problem.

If this package helps you, consider:

  • โญ Starring the GitHub repo
  • ๐Ÿ“ข Sharing this article
  • โ˜• Buying me a coffee (if you're feeling generous)

Connect with me:

  • ๐ŸŒ Website: jiordiviera.me
  • ๐Ÿ™ GitHub: @jiordiviera
  • ๐Ÿ’ผ LinkedIn: [Add your LinkedIn if you have one]
  • ๐Ÿฆ Twitter/X: [Add your handle if you have one]

Other projects:

  • Codepit - A minimal platform to share code snippets

Thanks for reading! If you found this helpful, please leave a โค๏ธ and share your experience in the comments below.

Happy logging! ๐Ÿš€

Laravel #PHP #OpenSource #DevOps #WebDev #BackendDevelopment

Top comments (0)