It is important to have a proper log rotation that prevents disk space issues and keeps system running smoothly. If you are running a Laravel application on a Linux system, you should also implement a proper log rotation policy. Here in this article we will discuss 3 ways to rotate Laravel logs.
Method 1: Use Laravel’s Built-in Daily Log Rotation
Laravel has a built-in log rotation options. You just need to configure Laravel to auto-rotate logs daily without external tools.
Edit the config/logging.php file in editor and add the following content:
'channels' => [ 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', 'days' => 7, ], ], Next set the default channel in .env file:
LOG_CHANNEL=daily Method 2: Set Up System-Level Logrotate for Laravel Logs
On Linux systems, we can also use logrotate service to manage large log files at the OS level. You just need to a configuration file below:
sudo nano /etc/logrotate.d/laravel Add the following content:
/var/www/your-laravel-app/storage/logs/*.log { daily missingok rotate 7 compress delaycompress notifempty create 664 www-data www-data sharedscripts postrotate php /var/www/your-laravel-app/artisan log:clear >/dev/null 2>&1 || true endscript } Make sure to change your-laravel-app path in above file.
Now, dry-run and/or force rotation to ensure config works correctly.
# Test configuration (debug mode) sudo logrotate -d /etc/logrotate.d/laravel # Force immediate rotation sudo logrotate -f /etc/logrotate.d/laravel Method 3: Clear Old Logs Manually (Optional)
For the development machines, you can simple use Artisan command to clear current log file when needed.
php artisan log:clear
