承接 studio24/rotate 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

studio24/rotate

Composer 安装命令:

composer require studio24/rotate

包简介

File rotation utility which rotates and removes old files

README 文档

README

Simple file rotation utility which rotates and removes old files or folders, useful where you cannot use logrotate (e.g. a Windows system) or you want to rotate or delete files based on a timestamp or date contained in the filename.

Installation

composer require studio24/rotate

Usage

You can use Rotate in two modes: rotate (renames files and removes oldest files) or delete (deletes files according to a pattern).

Import at the top of your PHP script via:

use studio24\Rotate\Rotate;

Setting the filename format to match

Both Rotate and Delete pass in the filename pattern you want to match for in the constructor or via the setFilenameFormat() method. This can be used to match a single file, files matching a pattern, or files with a datetime within the filename pattern.

Rotate only works on files. Delete can also delete folders and recursively deletes any child files in that folder.

Matches on leaf elements

Please note files are matched on the last leaf element. All files in the parent folder are scanned, files (and folders for Delete) are checked to see whether they match the specified pattern.

For example passing path/to/*.log will search for all files ending .log in the folder path/to.

Filename patterns

The following patterns are supported when matching files or folders:

  • debug.log - exact match for a file called debug.log
  • * - matches any string, for example *.log matches all files ending .log
  • {Ymd} - matches time segment in a file, for example order.{Ymd}.log matches a file in the format order.20160401.log

Datetime formats

For datetime formats, any date format supported by DateTime::createFromFormat is allowed excluding the Timezone identifier e and whitespace and separator characters.

Deleting folders recursively

You can also delete folders and all child files that match. This is, however, dangerous so by default no folders can be deleted. You need to explicitly add folder paths that are safe to delete via Delete::addSafeRecursiveDeletePath($path). When using this function you need to use the full absolute path. Use realpath() to expand full paths if you need to.

For example, if you want to delete all folders in /var/www/test/staging/data/logs/ that are 1+ months old:

$rotate = new Delete('/var/www/test/staging/data/logs/old-logs/*');
$rotate->addSafeRecursiveDeletePath('/var/www/test/staging/data/logs/');
$files = $rotate->deleteByFilenameTime('1 month');

The above code would allow you to delete folders within /var/www/test/staging/data/logs/ only.

Rotate

Rotate log files in a similar manner to logrotate.

The following example rotates the file debug.log, this renames debug.log to debug.log.1, debug.log.1 to debug.log.2, debug.log.2 to debug.log.3 and so on. It keeps 10 copies, so it deletes debug.log.10 and renames debug.log.9 to debug.log.10.

use studio24\Rotate\Rotate;

$rotate = new Rotate('path/to/debug.log');
$rotate->run();

How many copies to keep

Rotate keeps 10 copies of files by default, you can change this via:

$rotate->keep(20);

Rotated based on filesize

You can only rotate files when they reach a certain filesize, rather than automatically rotate each time the $rotate->run() method is run.

$rotate->size("12MB");

Delete

Deletes files based on modification time, datetime in the filename, or based on a custom callback function.

Time-based

Deletes files based on the modification time. For example, to delete all JPG files in a folder over 3 months old:

use studio24\Rotate\Delete;

$rotate = new Delete('path/to/images/*.jpg');
$deletedFiles = $rotate->deleteByFileModifiedDate('3 months');

The deleteByFileModifiedDate() method accepts either a valid DateInterval object or a relative date format as specified on Relative Formats.

Time format in filename

Deletes files based on the datetime in the filename. For example, to delete all order logfiles with a date in their filename over 3 months old:

use studio24\Rotate\Delete;

$rotate = new Delete('path/to/logs/orders.YYYYMMDD.log');
$deletedFiles = $rotate->deleteByFilenameTime('3 months');

Based on a custom callback

Deletes files based on a custom callback function, this is useful if you need to perform more complex code to assess whether a file should be deleted. For example, to delete all image files called 1000.jpg and below:

use studio24\Rotate\Delete;
use studio24\Rotate\DirectoryIterator;

$rotate = new Delete('path/to/logs/*.jpg');
$deletedFiles = $rotate->deleteByCallback(function(DirectoryIterator $file){
    if ($file->getBasename() <= 1000) {
        return true;
    } 
    return false;
});
DirectoryIterator

The callback function must accept one parameter $file, which is of type \studio24\Rotate\DirectoryIterator.

This iterator has the following methods in addition to the normal SPL DirectoryIterator.

  • isMatch() - whether the current file matches the filename patter we are looking for
  • hasDate() - whether the current file has a datetime within the filename
  • getFilenameDate() - return the datetime from the current file (as a DateTime object)

License

The MIT License (MIT). Please see License File for more information.

Credits

studio24/rotate 适用场景与选型建议

studio24/rotate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 100.77k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2016 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rotate」 「logrotate」 「delete files」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 studio24/rotate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 studio24/rotate 我们能提供哪些服务?
定制开发 / 二次开发

基于 studio24/rotate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 100.77k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 29
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-08