In PHP's Laravel, how do I clear laravel.log?

Solution 1:

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

Solution 2:

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.

Solution 3:

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

Solution 4:

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