alkhatibdev/logrotation
Composer 安装命令:
composer require alkhatibdev/logrotation
包简介
Laravel package for easy log rotation
关键字:
README 文档
README
This package makes log file rotation easier by automatically managing and organizing Laravel logs based on time and file size, retaining only the most recent logs (e.g., 12 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 date (monthly) or file size (size-based rotation).
- Retains logs for a configurable number of months (default: 12 months), which can be modified through the configuration file.
- Automatically deletes old logs after the configured retention period, freeing up disk space.
- Supports both compressed (
.gz) and uncompressed log files. - Helps prevent large log files from consuming excessive disk space during high-traffic periods.
Installation
Install the package via Composer:
composer require alkhatibdev/logrotation
Configuration & Advanced Customization
You can configure the log rotation behavior either via your .env file or programmatically using the LogRotator class.
Using .env file
Set the following variables in your .env file:
# Number of months to retain logs (default: 12) LOG_ROTATION_MAX_MONTHS=12 # Maximum file size (in KB) for size-based rotation (optional, default: null, no size-based rotation) LOG_ROTATION_MAX_SIZE_KB=10240 # Enable or disable compression for archived logs (optional, default: true) LOG_ROTATION_COMPRESS=true
LOG_ROTATION_MAX_MONTHS: How many months of logs to keep before deletion.LOG_ROTATION_MAX_SIZE_KB: The size threshold for size-based rotation. When the log file exceeds this, a size-based archive is created.LOG_ROTATION_COMPRESS: Whether the archived logs should be compressed (.gz) or kept uncompressed (.log).
Note: You can still publish the configuration file if you want to override defaults or use dynamic configuration:
php artisan vendor:publish --tag=logrotation
Programmatic Customization
You can fully customize log rotation by using the LogRotator class:
use AlkhatibDev\LogRotation\LogRotator; LogRotator::make() // Set a custom log file path (default: storage/logs/laravel.log) ->setLogFile(storage_path('logs/custom.log')) // Set the maximum number of months to retain logs (default: 12) ->setMaxMonths(12) // Set the maximum file size in KB for size-based rotation (optional) (default: null, no size-based rotation) ->setMaxSize(10240) // 10 MB // Enable or disable compression for archived logs (.gz) (default: true) ->setCompression(true) // Perform the rotation immediately ->rotate();
This merged approach allows you to configure the package using .env for most cases or fine-tune behavior programmatically when needed.
Usage
Log Rotation Scheduling
You can schedule log rotation in routes/console.php:
Artisan::command('logrotation:rotate', function () { app('logrotator')->rotate(); })->daily();
Or for Laravel 10.x and below, in app/Console/Kernel.php:
protected function schedule(Schedule $schedule) { $schedule->call(function () { app('logrotator')->rotate(); })->daily(); }
How it works:
- Scheduling daily ensures size-based rotation works properly, rotating large log files as soon as they exceed the configured size.
- Monthly rotation will still run only once per month — if the current month has already been rotated, it will be ignored until the next month.
- Optional: If you don’t want to enable size-based rotation, you can run the command
monthly()instead. This will rotate logs only once per month based on time, ignoring size increasing during the month.
Monthly Log Archives
The rotation creates a timestamped archive for the current file:
file-YYYY-MM-monthly.log
file-YYYY-MM-monthly.log.gz (if compression is enabled)
Size-Based Log Rotation
The package now supports automatic rotation when the log file exceeds a specified size. This is useful for high-traffic applications where a single month’s log may grow too large.
The rotation creates a timestamped archive for the current file:
file-YYYY-MM_DD-HH-MM-SS-size.log
file-YYYY-MM_DD-HH-MM-SS-size.log.gz (if compression is enabled)
Size-based logs are also subject to month-based retention and will be deleted if their month is older than the configured max_months.
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.
alkhatibdev/logrotation 适用场景与选型建议
alkhatibdev/logrotation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 694 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「laravel」 「rotate」 「logrotation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alkhatibdev/logrotation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alkhatibdev/logrotation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alkhatibdev/logrotation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Powerful PHP class using GD library to work easily with images including layer notion (like Photoshop or GIMP)
A library for manipulating images in PHP.
A Filament plugin for displaying image galleries with zoom, rotate, and flip capabilities
Powerful PHP class using GD library to work easily with images including layer notion (like Photoshop or GIMP)
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
统计信息
- 总下载量: 694
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-12