Introduction:

Whenever any exceptions are thrown by laravel application, it will be added in laravel.log file on the storage directory. After some time, the size of the file increases and it may create the space issue or memory issue in the server. We will discuss various solutions to clear the laravel.log file to prevent those kinds of issues in the production server.

Description:

We can clear or empty the laravel.log file and for that we will discuss various methods to do it.

Preferred Solution:

We can make artisan commands so that it will get executed in a certain time period and we don’t have to remember to execute the commands every time.

Put this in routes/console.php then execute php artisan logs:clear

Artisan::command('logs:remove, function() {    
    exec('rm -f ' . storage_path('logs/*.log'));
    exec('rm -f ' . base_path('*.log'));    
    $this->comment('Logs have been removed!');    
})->describe('Remove log files);

Other Ways to clear logs from terminal:

1. Echoing an empty string to logs:

echo "" > storage/logs/laravel.log

2. Try to use truncate it to make size of zero :

truncate -s 0 /app/storage/logs/laravel.log

3. Using vim

$ vim laravel.log

Press keys:- ggdG
Press keys:- :wq & save and quit.

Explanation: gg – moves cursor to first line
d – delete
G – to the end of the file

4. Using Tinker
Execute command in terminal

php artisan tinker

Then execute

use Illuminate\Support\Facades\Redis;
Redis::resolve('horizon')->flushall();

Expected response:

> Redis::resolve('horizon')->flushall(); = true

5 Configure Log Rotation:

  • Laravel supports log rotation, which allows you to automatically manage log files and keep them within a specified size or time limit.
  • Open the config/logging.php file and locate the ‘channels’ configuration section.
  • Modify the configuration for the log channel you’re using (e.g., ‘single’, ‘daily’, etc.) and set the ‘days’ or ‘max’ value according to your requirements.
  • For example, to rotate logs daily, you can use the ‘daily’ channel with the ‘days’ parameter set to a desired number of days:
  • Code snippet
  • 'channels' => [
        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'days' => 7, // Rotate logs every 7 days
        ],
        // Other channels
    ],
    
  • This configuration will automatically rotate logs based on the specified interval.

6 Use Laravel’s Artisan Command:
Laravel provides an Artisan command to clear log files. Open your terminal and run the following command within your Laravel project directory:
php artisan log:clear

7 Use Log Management Packages:
There are several third-party packages available that offer enhanced log management features for Laravel.
For example, you can use the spatie/laravel-log-cleanup package to automatically clean up log files based on various criteria such as file size, date, and more.

8 Manually Delete Log Files:
In Linux/Mac, you can run the following command to delete log files older than a certain number of days (e.g., 7 days):
find /path/to/your/laravel/storage/logs -type f -mtime +7 -delete

Support On Demand!

                                         
Laravel