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

15 Answers

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

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

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

1

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

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

This worked for me :

echo "" > storage/logs/laravel.log 
1

From your Laravel directory:

rm storage/logs/laravel-*.log 
1

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

For Laravel 5, it would be

truncate -s 0 storage/logs/laravel.log 

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

6

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.

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.

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 

Go to tinker php artisan tinker

Then run

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

Expected response:

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

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'); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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