承接 jalsoedesign/log-stream 相关项目开发

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

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

jalsoedesign/log-stream

最新稳定版本:0.5.2

Composer 安装命令:

composer require jalsoedesign/log-stream

包简介

README 文档

README

Simple class that takes a path to a file and gives you the following features:

  • $stream->readLines(10) to get the first 10 lines as a string
  • $stream->getLines(10) to get the first 10 lines as an array
  • $stream->readLines(-10) to get the last 10 lines as a string
  • $stream->getLines(-10) to get the last 10 lines as an array
  • $stream->readIncremental() to get new content added to the file (as it's written to)

Also gives you a simple way of seeking lines:

  • $stream->seekLines(10) to seek to the end of the first 10 lines
  • $stream->seekLines(-10) to seek to the beginning of the last 10 lines
  • $stream->seekReset() to reset the seek to the first byte
  • $stream->seekEnd() to seek to the end

Note: Using a negative offset will return the lines in reverse order (last line first). This can be prevented by setting $returnOriginalOrder to true as such: $stream->readLines(-10, true)

Installation

composer require jalsoedesign/log-streamer

Usage

Instantiation

use jalsoedesign\LogStream\LogStream;

require_once('vendor/autoload.php');

$stream = LogStream::fromPath('foo.txt');

// OR

$handle = fread('foo.txt', r);
$stream = new LogStream($handle);

First 10 lines

Read the first 10 lines of a file and print them as text

$stream = LogStream::fromPath('foo.txt');

echo 'The first 10 lines:' . PHP_EOL;
echo $stream->readLines(10);

Read the first 10 lines of a file and print them as an array

$stream = LogStream::fromPath('foo.txt');

echo 'The first 10 lines:' . PHP_EOL;
print_r($stream->getLines(10));

Last 10 lines

Read the last 10 lines of a file and print them as text

$stream = LogStream::fromPath('foo.txt');

echo 'The last 10 lines:' . PHP_EOL;
echo $stream->readLines(-10);

Read the last 10 lines of a file and print them as an array

$stream = LogStream::fromPath('foo.txt');

echo 'The last 10 lines:' . PHP_EOL;
print_r($stream->getLines(-10));

Tail functionality

Read the last 10 lines of a file and then continue to printing content when the file gets updated

$stream = LogStream::fromPath('foo.txt');

$logStream->seekLines(-10);

$running = true;

echo 'The last 10 lines as well as all new content:' . PHP_EOL;

do {
    echo $logStream->readIncremental();

    usleep(100000);
} while ($running);

Open a stream and only print the bytes that have been added to the file after opening

$stream = \LogStream::fromPath('foo.txt');

$logStream->seekReset();

$running = true;

echo 'New file content:' . PHP_EOL;

do {
    echo $logStream->readIncremental();

    usleep(100000);
} while ($running);

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-11