This content originally appeared on DEV Community and was authored by DON EJEH
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.
This content originally appeared on DEV Community and was authored by DON EJEH
DON EJEH | Sciencx (2021-04-11T20:13:39+00:00) Improve performance by deleting laravel log file via Routes. (Artisan & exec). Retrieved from https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.