whatsapp_btn
whatsapp_btn Chat With Us

Home >> Laravel Q&A >> Ways to Clear Laravel Logs

Ways to Clear Laravel Logs

  6 min read
Ways to Clear Laravel Logs

Introduction

You will see that whenever different types of exceptions are tossed into a Laravel application they will be automatically added to a laravel.log file in the storage directory. Soon the size of that file will increase so you may face a memory issue in the server. Don’t worry we have got you covered here. You can see different ways to clear Laravel logs to reduce the size of the laravel.log file for better performance of the production server.

Method to clear laravel logs

The developers can easily clear the laravel.log file and here you can explore various methods to do it.

Most Common Preferred Solution

See the command that is given below which developers can run for a certain period and that command will be executed automatically.

Simply add the below code 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 the Terminal

1. Echo Empty String into logs


echo "" > storage/logs/laravel.log

2. Use truncate it to make size of zero


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

3. Use vim command


$ vim laravel.log

Press keys:- ggdG

Press keys:- :wq & save and quit.

Gg command is used to move the cursor for the first time.

d command is used to delete.

G command is used to end the file.

4. Use Tinker and execute the command in Terminal


php artisan tinker

After that, you need to execute this:

use Illuminate\Support\Facades\Redis;

Redis::resolve(‘horizon’)->flushall();

You will receive this expected response:

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

5. Configure Log Rotation

  • Laravel provides support for log rotation so that developers can automatically manage the log files to reduce the overall size and time limit during execution.
  • Simply open the config/logging.php file to quickly locate the channels from the configuration section.
  • The developers can even modify the configuration for the log channel by using single, daily, and many more to set the days or max value as per their requirements for the project.
  • Here you can see a code that can help to automatically rotate the logs based on the specific intervals mentioned below.

'channels' => [

'daily' => [

     'driver' => 'daily',

     'path' => storage_path('logs/laravel.log'),

     'days' => 7, // Rotate logs every 7 days

],

// Other channels

],

6. Use Laravel’s Artisan Command

You just need to open the terminal and use Laravel’s artisan command to clear log files. Just write this command in the terminal according to your Laravel project’s directory. 

php artisan log:clear

7. Use Log Management Packages

Nowadays in the market, you can see different types of third-party packages for Laravel to improve the log management process. You can simply use spatie/laravel-log-cleanup package to clean up the log files of your Laravel project based on factors like its size, date, time, and many more.

8. Manually Delete Log Files

If you are using Mac or Linux then you can simply run the command given below to delete the log files that are older than the days mentioned in the code and here it is 7.

find /path/to/your/laravel/storage/logs -type f -mtime +7 -delete

Tagline Infotech
Tagline Infotech a well-known provider of IT services, is deeply committed to assisting other IT professionals in all facets of the industry. We continuously provide comprehensive and high-quality content and products that give customers a strategic edge and assist them in improving, expanding, and taking their business to new heights by using the power of technology. You may also find us on LinkedIn, Instagram, Facebook and Twitter.

Related Posts :

contact-us-bg

Our Global Presence

India (HQ)

Digital Valley, 423, Apple Square, beside Lajamni Chowk, Mota Varachha, Surat, Gujarat 394101

 +91 9913 808 285

U.S.A

1133 Sampley Ln Leander, Texas, 78641

United Kingdom

52 Godalming Avenue, wallington, London - SM6 8NW