alkhatibdev / logrotation
Laravel package for easy log rotation
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
README
This package makes log file rotation easier by automatically managing and organizing Laravel logs based on file creation date, retaining only the most recent logs (e.g., 6 months) and discarding older logs. This solution is ideal for applications that generate high volumes of logs, providing efficient log management and preventing excessive disk usage over time.
Benefits
- Rotates your log files based on the log file's current date, ensuring that only the most recent logs are retained.
- Retain logs for a configurable number of months (default: 6 months), which can easily be modified through the configuration file.
- Automatically deletes old logs after the configured retention period, freeing up space on your server.
Installation
Install the package via Composer:
composer require alkhatibdev/logrotation
Configuration
To publish the configuration file, run the following command:
php artisan vendor:publish --tag=logrotation
This will create a logrotation.php
file in your config/
directory where you can customize the number of months to retain logs:
return [ 'max_months' => env('LOG_ROTATION_MAX_MONTHS', 6), ];
Usage
Once installed, you can integrate the log rotation into your application's task scheduling. To rotate logs on a monthly basis, open your app/Console/Kernel.php
file and add the following to the schedule()
method:
protected function schedule(Schedule $schedule) { $schedule->call(function () { app('logrotator')->rotate(); })->monthly(); }
For Laravel 11.x, You can create scheduled command on routes/console.php
file:
Artisan::command('logrotation:rotate', function () { app('logrotator')->rotate(); })->monthly();
This will ensure that logs are rotated and older logs are deleted automatically at the beginning of every month.
Manual Log Rotation
You can also trigger log rotation manually by running:
php artisan schedule:run
This will immediately check and rotate logs if necessary.
Advanced Customization
In case you want to change the default log location or customize the log retention behavior, you can extend the LogRotator
class and override its methods. By default, the package manages the storage/logs/laravel.log
file, but you can pass a custom log file path when initializing the class:
use AlkhatibDev\LogRotation\LogRotator; $logRotator = new LogRotator(); $logRotator ->setLogFile(storage_path('logs/custom.log')) // Set the log file path to rotate ->rotate();
Support
If you encounter any issues or have feature requests, feel free to open an issue on GitHub.
License
This package is open-sourced software licensed under the MIT license.