innmind / log-reader
解析日志文件
5.3.0
2023-10-22 12:47 UTC
Requires
- php: ~8.2
- innmind/filesystem: ~7.0
- innmind/http: ~7.0
- innmind/immutable: ~4.9|~5.0
- innmind/json: ^1.1
- innmind/time-continuum: ~3.1
- innmind/url: ~4.1
- psr/log: ^3.0
Requires (Dev)
- innmind/coding-standard: ~2.0
- phpunit/phpunit: ~9.0
- vimeo/psalm: ~5.12
README
允许您解析 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 行的 context
或 extra
属性失败,它们将不会作为属性在 Log
对象中暴露。这种行为是为了防止整个解析因为这种错误而失败。
注意 2:如果由于某种原因无法解析一行,它将简单地被忽略,并不会暴露。这种行为也是为了防止整个解析因为这种错误而失败。