DEV Community

Cover image for Improve performance by deleting laravel log file via Routes. (Artisan & exec)
EJEH
EJEH

Posted on • Updated on

Improve performance by deleting laravel log file via Routes. (Artisan & exec)

Laravel log file helps we developers in so many ways such logging of data and more importantly is that all errors generated by our web application are be logged in our laravel log file which is located at storage\logs\laravel.log, this error generated by our web app can make log file size rapidly increasing to like 30GB in size 😲 according to this post => Laravel log file size rapidly increasing, this can be a bottle neck problem to some simple Laravel application out there that is been hosted in a small web shared hosting site with limit disk space. Instead of using ssh or filezilla to delete this log file manually over and over, here i will show you how to do that with via route i.e when you goto www.yoursite.come/clear or www.yoursite.come/delete that log file will be deleted and your server will have a breathing space. 👽

Let get started either by using existing project or creating new project this will still work.

Goto your web.php file and paste this

Route::get('/clear', function() {
exec('rm -f ' . storage_path('logs/.log'));
exec('rm -f ' . base_path('
.log'));
return "Log file deleted";
});

To run the Above route goto www.yoursite.com/clear and hit enter, BOOM it delete all the log file in storage_path or base_path. So also you can change this to any location you log file is kept. 😁

For bonus let me show you a simple way to run Artisan inside route (web.php or api.php) on live production with using terminal. WIth this Artisan command you can optimize your laravel web app.

Route::get('/cl', function() {
Artisan::call('config:cache');
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('view:clear');
Artisan::call('route:clear');
exec('rm -f ' . storage_path('logs/.log'));
exec('rm -f ' . base_path('
.log'));
return "Cache is cleared";
})->name('clear.cache');

Above command helps you to Improve Laravel Performance by removing Config caching, Routes caching and view caching for better loading of your web app. 😜

Post Motivation
My laravel.log size is 31 GB.

For more tutorial share , comment and if any question or suggest drop it.

Follow me on X for more.. Ejeh on X

Top comments (0)