PHP code example of innmind / log-reader

1. Go to this page and download the library: Download innmind/log-reader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innmind / log-reader example snippets


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'),
    );