69

I'm new to Laravel and PHP in general, and I'm trying to clear my errorLog. The current errorlog that I'm using is Laravel's laravel.log file that is in /app/storage/logs.

Is there an easy way to clear the laravel.log file? Is it safe to delete it and it'll rebuild when needed?

I'm using the latest version of Ubuntu. Thanks!

3
  • 2
    cat /dev/null > /app/storage/logs/laravel.log - unix.stackexchange.com/questions/92384/how-to-empty-a-log-file Commented Jan 24, 2015 at 16:22
  • 1
    echo "" > /app/storage/logs/laravel.log also works, and the most efficient is truncate -s 0 /app/storage/logs/laravel.log. More info : unix.stackexchange.com/questions/88808/…
    – user2629998
    Commented Jan 24, 2015 at 16:33
  • If you delete it, the laravel.log file will automatically regenerate once an entry tries to write to it. So yes you can rm storage\logs\laravel.log if you know nothing else needs it, and it will reappear clean and new.
    – Marsellus
    Commented May 11, 2023 at 13:55

16 Answers 16

136

Echoing an empty string to it would work, like this :

echo "" > storage/logs/laravel.log

The most efficient would be to truncate it to a size of zero :

truncate -s 0 /app/storage/logs/laravel.log
3
  • 4
    Depending on your OS you might have to use ./ and not just a / for the current directory truncate -s 0 ./storage/logs/laravel.log
    – mtpultz
    Commented Apr 2, 2018 at 18:09
  • use @Adnan Rasheed 's answer to clear logs from your controller , meaning from browser address string.
    – CodeToLife
    Commented Jun 13, 2021 at 1:08
61

Here's a reusable artisan command that will save you time and effort :)

Artisan::command('logs:clear', function() {
    
    exec('rm -f ' . storage_path('logs/*.log'));

    exec('rm -f ' . base_path('*.log'));
    
    $this->comment('Logs have been cleared!');
    
})->describe('Clear log files');

Drop this in routes/console.php then just run php artisan logs:clear


Updates:

  • Added base_path('*.log') for npm, composer etc logs in root directory.
  • Added -f to rm function to suppress the No such file or directory message.
4
  • 1
    getting the error:"'rm' is not recognized as an internal or external command"
    – Naim R.
    Commented Jun 14, 2021 at 20:18
  • @NaimR. I'm assuming you are using Windows? This is for Linux/macOS sorry, don't know anything about Windows.
    – emotality
    Commented Jun 19, 2021 at 20:47
  • I will add this line at the end so the log file is created again exec('touch ' . storage_path('logs/laravel.log'));
    – Fakher
    Commented Sep 22, 2021 at 21:18
  • @Fakher why create it again if Laravel creates it for you? If you want to empty it out just do this: echo > laravel.log
    – emotality
    Commented Sep 24, 2021 at 19:35
20

Simply just add this line where you want to clear the log. file_put_contents(storage_path('logs/laravel.log'),'');

1
  • This worked perfect in my helpers.php file along side other custom logging functions.
    – isimmons
    Commented Aug 17, 2022 at 3:56
18

You can also create custom artisan command.

First, run command php artisan make:command Log/ClearLogFile to create custom command file.

Then, open file on Console/Commands/Log/ClearLogFile.php (depends on you Laravel version, currently I'm using 5.5 version)

After that, you need to define the custom command code, take a look

// Define command name
protected $signature = 'log:clear';

// Add description to your command
protected $description = 'Clear Laravel log';

// Create your own custom command
public function handle(){
    exec('echo "" > ' . storage_path('logs/laravel.log'));
    $this->info('Logs have been cleared');
}

Then, you only need to run just like other php artisan command,

php artisan log:clear

Thanks, for @emotality answer

2
  • I got error when I run php artisan log:clear "There are no commands defined in the "log" namespace".
    – Nurul
    Commented Jan 3, 2022 at 3:57
  • @Nurul Looks like you defined the wrong namespace. First, make sure your file ClearLogFile.php are in the path app/Console/Commands/Log/ClearLogFile.php. Second, please check the namespace (at the top of code), make sure it looks like this namespace App\Console\Commands\Log;. Then try run php artisan list to check is the command available. If error still occured, please check this one, stackoverflow.com/q/32396158/4494207
    – ibnɘꟻ
    Commented Jan 3, 2022 at 6:35
16

Easiest way for me is using vim.

$ vim laravel.log

Then type ggdG

Then :wq save and quit.

Notes: gg - moves cursor to first line

d - delete

G - to the end of the file

1
  • 10
    Thanks for not just giving the solution, but explaining the arguments. You may have a bright future on StackOverflow! :) Commented Jul 19, 2018 at 15:28
11

This worked for me :

echo "" > storage/logs/laravel.log
1
  • This is simplest solution! Nice!
    – bandungeuy
    Commented Oct 19, 2021 at 1:09
5

From your Laravel directory:

rm storage/logs/laravel-*.log
1
  • we can't simply delete the file, other services might be using those logs and deleting the file might cause issues
    – NxKai
    Commented May 7, 2019 at 2:38
5

I found this solution works well for windows

Artisan::command('logs:clear', function() {
   array_map('unlink', array_filter((array) glob(storage_path('logs/*.log'))));
   $this->comment('Logs have been cleared!');
})->describe('Clear log files');

Run php artisan logs:clear

0
4

For Laravel 5, it would be

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

To complete @emotality response. Here is the code for Windows.

Artisan::command('logs:clear', function() {
    exec('echo "" > ' . storage_path('logs/laravel.log'));
    $this->info('Logs have been cleared');
})->describe('Clear log files');

Drop it in routes/console.php And use it like this:

php artisan logs:clear
2

You can also do: : > storage/logs/laravel.log

6
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – Sree
    Commented Mar 26, 2018 at 10:02
  • How does this not provide answer to the question?
    – Nicolas
    Commented Mar 28, 2018 at 10:17
  • The question is already answered long ago and it is accepted, what is the intention of adding a new answer to this question without any explanation?
    – Sree
    Commented Mar 28, 2018 at 10:26
  • 1
    your solution is always welcome but how this differs from the accepted answer and you are not given any explanation to your answer.
    – Sree
    Commented Mar 29, 2018 at 3:28
  • 1
    1. Sree what a jacka$$ 2. This is the best solution to the problem Commented Aug 10, 2022 at 15:50
2

There is very easy way to clear log with php artisan log:clear command using Laravel More Command Package.

First Install Laravel More Command

composer require theanik/laravel-more-command --dev

Then Run

php artisan log:clear

The above will deleted all old log data from /storage/logs/ directory.

Thank you.

1

I usually stick the following line in my controller method when I am running the SQL query listener:

exec("truncate -s 0 " . storage_path('/logs/laravel.log'));

If you use a different monolog channel setup you might want to tweak the name of your log file.

0

Go to tinker php artisan tinker

Then run

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

Expected response:

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

I just add a bit to be sure before clearing.

Artisan::command('log:clear', function() {
    if ($this->confirm('Do you wish to continue?')) {
        exec('echo "" > ' . storage_path('logs/laravel.log'));
        $this->info('Logs have been cleared');        
    }
})->describe('Clear Laravel log');
0
Artisan::command('logs:clear', function() {
    
    foreach (glob(storage_path('logs/*.log')) as $file) {
        file_put_contents($file, '');
    }
    
    foreach (glob(base_path('*.log')) as $file) {
        file_put_contents($file, '');
    }
    
    $this->comment('Logs have been cleared!');
    
})->describe('Clear log files');

I use this for Laravel 11, it may work for you.

After defining the route -> console.php file, we can run it as follows.

php artisan log:clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.