How do I Print an Exception in Python?
April 29, 2024
Home >> Laravel Q&A >> Ways to Clear Laravel Logs
Introduction
You will see that whenever different types of exceptions are tossed into a Laravel application they will be automatically added to a laravel.log file in the storage directory. Soon the size of that file will increase so you may face a memory issue in the server. Don’t worry we have got you covered here. You can see different ways to clear Laravel logs to reduce the size of the laravel.log file for better performance of the production server.
The developers can easily clear the laravel.log file and here you can explore various methods to do it.
See the command that is given below which developers can run for a certain period and that command will be executed automatically.
Simply add the below code in routes/console.php then execute php artisan logs: clear
Artisan::command('logs:remove, function() {
exec('rm -f ' . storage_path('logs/*.log'));
exec('rm -f ' . base_path('*.log'));
$this->comment('Logs have been removed!');
})->describe('Remove log files);
1. Echo Empty String into logs
echo "" > storage/logs/laravel.log
2. Use truncate it to make size of zero
truncate -s 0 /app/storage/logs/laravel.log
3. Use vim command
$ vim laravel.log
Press keys:- ggdG
Press keys:- :wq & save and quit.
Gg command is used to move the cursor for the first time.
d command is used to delete.
G command is used to end the file.
4. Use Tinker and execute the command in Terminal
php artisan tinker
After that, you need to execute this:
use Illuminate\Support\Facades\Redis;
Redis::resolve(‘horizon’)->flushall();
You will receive this expected response:
> Redis::resolve(‘horizon’)->flushall(); = true
5. Configure Log Rotation
'channels' => [
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'days' => 7, // Rotate logs every 7 days
],
// Other channels
],
6. Use Laravel’s Artisan Command
You just need to open the terminal and use Laravel’s artisan command to clear log files. Just write this command in the terminal according to your Laravel project’s directory.
php artisan log:clear
7. Use Log Management Packages
Nowadays in the market, you can see different types of third-party packages for Laravel to improve the log management process. You can simply use spatie/laravel-log-cleanup package to clean up the log files of your Laravel project based on factors like its size, date, time, and many more.
8. Manually Delete Log Files
If you are using Mac or Linux then you can simply run the command given below to delete the log files that are older than the days mentioned in the code and here it is 7.
find /path/to/your/laravel/storage/logs -type f -mtime +7 -delete
Digital Valley, 423, Apple Square, beside Lajamni Chowk, Mota Varachha, Surat, Gujarat 394101
D-401, titanium city center, 100 feet anand nagar road, Ahmedabad-380015
+91 9913 808 2851133 Sampley Ln Leander, Texas, 78641
52 Godalming Avenue, wallington, London - SM6 8NW