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
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
Keep the last 30 days:
php artisan log:clear --days=30
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
Output:
[DRY RUN] Would remove 15,420 lines from laravel.log
[DRY RUN] Estimated space to free: 45.2 MB
Create a backup before cleaning:
php artisan log:clear --days=30 --backup
Output:
Backup created: storage/logs/laravel.log.backup.2024-11-05-14-30-15
Logs older than 30 days have been removed.
2. Compression Instead of Deletion ๐ฆ
Archive old logs for compliance or audit purposes:
php artisan log:clear --days=30 --compress
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
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
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})/"
6. Combine Everything ๐
The complete workflow:
php artisan log:clear --days=30 --backup --compress --level=ERROR --dry-run
This command:
- Previews what would be deleted (dry-run)
- Creates a backup before making changes
- Compresses old logs instead of deleting
- 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');
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');
}
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
- 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
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
2. Production: Aggressive Cleanup with Safety
php artisan log:clear --days=7 --backup --level=ERROR
3. Compliance: Archive Everything
php artisan log:clear --days=90 --compress
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
5. CI/CD Pipeline: Clean After Tests
php artisan log:clear --days=0
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
Links:
- ๐ฆ Packagist
- ๐ GitHub Repository
- โญ Star on GitHub if you find it useful!
Contributing
Found a bug? Have a feature request? Contributions are welcome!
- Fork the repo
- Create a feature branch
- Write tests (we use Pest)
- 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! ๐
Top comments (0)