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

PhpLaravel 4

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

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 - Php

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 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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

Solution 6 - Php

This worked for me :

echo "" > storage/logs/laravel.log

Solution 7 - Php

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

Solution 8 - Php

For Laravel 5, it would be

truncate -s 0 storage/logs/laravel.log

Solution 9 - Php

From your Laravel directory:

rm storage/logs/laravel-*.log

Solution 10 - Php

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

Solution 11 - Php

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.

Solution 12 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionObelisk47View Question on Stackoverflow
Solution 1 - Phpuser2629998View Answer on Stackoverflow
Solution 2 - PhpemotalityView Answer on Stackoverflow
Solution 3 - PhpAdnan RasheedView Answer on Stackoverflow
Solution 4 - PhpYulinView Answer on Stackoverflow
Solution 5 - PhpibnɘꟻView Answer on Stackoverflow
Solution 6 - PhpKévin FerradjView Answer on Stackoverflow
Solution 7 - PhpPranta SahaView Answer on Stackoverflow
Solution 8 - PhpFaridul KhanView Answer on Stackoverflow
Solution 9 - PhpJosh LaMarView Answer on Stackoverflow
Solution 10 - PhpNicolasView Answer on Stackoverflow
Solution 11 - PhpPatrick.SEView Answer on Stackoverflow
Solution 12 - PhpAnik AnwarView Answer on Stackoverflow