Skip to content

How to Rotate Laravel Log Files

Category
PHP
Covers
Laravel
Versions & support
About PHP →
On this page

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

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top