Home » How to Clear Laravel Log: 5 Proven Ways to Do That

How to Clear Laravel Log: 5 Proven Ways to Do That

Loved it? Please Share on Social Media

Introduction:

Hello, fellow developers. What’s up?, I hope you are doing fine. In this extended tutorial, we will learn how to clear laravel log. We will discuss seven tested & proven methods for removing log files in Laravel.

Let’s remove our log files using a fresh Laravel application you downloaded earlier. Then, you must follow the rest of the process below.

How to Clear Laravel Log using Terminal Command:

You can remove your laravel log using a simple rm command for remove. So, we will remove our Laravel log folder using the command; please copy& paste the following command into your terminal & hit enter from your keyboard.

cd C:/xampp/htdocs/my_project
rm storage/logs/*.log

How to Clear Laravel Log using Artisan Command:

We can also use a default artisan command provided by the Laravel framework to remove our application log files. Please copy& paste the following command into your terminal & hit enter from your keyboard.

php artisan log:clear

How to Clear Laravel Log using Custom Artisan Command:

Another way of clearing laravel log files is using a custom artisan command. We can manually remove our application log files using this command or a cron job for automatic deletion. So, we will create a command file using the below command.

php artisan make:command ClearAppLogs

Now, open the ClearAppLogs file from the App/Console/Commands directory. Then copy & paste the following code into that file.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class ClearAppLogs extends Command

{

    protected $signature = 'logs:clear';

    protected $description = 'Clear Laravel Application Logs';

    public function handle() {
        $logPath = storage_path('logs');
        // Check if the logs directory exists
        if (File::exists($logPath)) {
            // Clear all log files
            File::delete($logPath . '/*.log');
            $this->info('Application Logs Cleared Successfully.');
        } else {
            $this->error('Application Logs Directory Not Found.');
        }
    }
}

Next, you have to register this command inside the app/Console/Kernel.php file. copy & paste the following line code inside the $protected $commands array.

protected $commands = [
    // Other commands...
    \App\Console\Commands\ClearAppLogs::class,
];

Now, we can use this command on our terminal, as shown below.

php artisan logs:clear

How to Clear Laravel Log using Log Rotation:

We use log rotation to remove our Laravel application log files. We have to configure our config>logging.php file for that. You can configure your logging file as shown below.

'daily' => [
       'driver' => 'daily',
       'path' => storage_path('logs/laravel.log'),
       'level' => env('LOG_LEVEL', 'debug'),
       'days' => 18,
       'replace_placeholders' => true,
],

How to Clear Laravel Log Using Laravel Telescope Package:

You can clear your laravel log files using Laravel first party telescope package. If you prefer to avoid removing your Laravel log using other ways, which I told you earlier, you can use this method more innovatively.

Just install the Laravel telescope package from the below commands. You must follow the step-by-step instructions while installing this package.

composer require laravel/telescope --dev
php artisan telescope:install
php artisan migrate

Finally, you must type the following command into your terminal and hit enter from your keyboard.

php artisan telescope:prune

Tips:

I’ve explained nine ways to clear laravel log in this tutorial. Sometimes, some tips will work, and some will not work. You must check every way I’ve told you to follow and practice. You can learn more about this topic from their documentation also.

Conclusion:

Finished! That’s all about our tutorial. I’ve tried to teach you the most updated & tested things in this tutorial. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.