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 …

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.


Print Share Comment Cite Upload Translate
APA
DON EJEH | Sciencx (2024-03-28T18:19:12+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/.
MLA
" » Improve performance by deleting laravel log file via Routes. (Artisan & exec)." DON EJEH | Sciencx - Sunday April 11, 2021, https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/
HARVARD
DON EJEH | Sciencx Sunday April 11, 2021 » Improve performance by deleting laravel log file via Routes. (Artisan & exec)., viewed 2024-03-28T18:19:12+00:00,<https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/>
VANCOUVER
DON EJEH | Sciencx - » Improve performance by deleting laravel log file via Routes. (Artisan & exec). [Internet]. [Accessed 2024-03-28T18:19:12+00:00]. Available from: https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/
CHICAGO
" » Improve performance by deleting laravel log file via Routes. (Artisan & exec)." DON EJEH | Sciencx - Accessed 2024-03-28T18:19:12+00:00. https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/
IEEE
" » Improve performance by deleting laravel log file via Routes. (Artisan & exec)." DON EJEH | Sciencx [Online]. Available: https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/. [Accessed: 2024-03-28T18:19:12+00:00]
rf:citation
» Improve performance by deleting laravel log file via Routes. (Artisan & exec) | DON EJEH | Sciencx | https://www.scien.cx/2021/04/11/improve-performance-by-deleting-laravel-log-file-via-routes-artisan-exec/ | 2024-03-28T18:19:12+00:00
https://github.com/addpipe/simple-recorderjs-demo