innmind /log-reader

解析日志文件

5.3.0 2023-10-22 12:47 UTC

This package is auto-updated.

Last update: 2024-09-22 14:40:33 UTC


README

Build Status codecov Type Coverage

允许您解析 symfony 和 apache 访问日志。

请注意,根据数据量的大小,这可能会花费很多时间(对于一个典型的 symfony dev.log,在10k行之后开始真正变慢)

安装

composer require innmind/log-reader

使用方法

use Innmind\LogReader\{
    Reader,
    LineParser\Monolog,
    Log,
};
use Innmind\OperatingSystem\Factory;
use Innmind\Filesystem\Name;
use Innmind\Url\Path;
use Psr\Log\LogLevel;

$os = Factory::build();

$read = Reader::of(
    Monolog::of($os->clock()),
);
$os
    ->filesystem()
    ->mount(Path::of('var/logs/'))
    ->get(Name::of('prod.log'))
    ->map(static fn($file) => $file->content())
    ->map($read)
    ->map(
        static fn($logs) => $logs
            ->filter(
                static fn($log) => $log
                    ->attribute('level')
                    ->filter(static fn($level) => $level->value() === LogLevel::CRITICAL)
                    ->match(
                        static fn() => true,
                        static fn() => false,
                    ),
            )
            ->foreach(
                static fn($log) => $log
                    ->attribute('message')
                    ->match(
                        static fn($attribute) => print($attribute->value()),
                        static fn() => print('No message found'),
                    ),
            ),
        static fn() => print('File does not exist'),
    );

上面的示例将打印所有在临界级别记录的消息。

注意:如果解析 monolog 行的 contextextra 属性失败,它们将不会作为属性在 Log 对象中暴露。这种行为是为了防止整个解析因为这种错误而失败。

注意 2:如果由于某种原因无法解析一行,它将简单地被忽略,并不会暴露。这种行为也是为了防止整个解析因为这种错误而失败。