PHP code example of stk2k / stream-logger

1. Go to this page and download the library: Download stk2k/stream-logger 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/ */

    

stk2k / stream-logger example snippets


use Stk2k\MemoryLogger\SimpleMemoryLogger;

$logger = new SimpleMemoryLogger();
$logger->debug('Hello, world!');
$logger->warning('Something is wrong...');

print_r($logger->toArray());
// Array
// (
//     [0] => 2019-10-11 20:42:10 [debug] Hello, world! []      @...\example1.php(7)
//     [1] => 2019-10-11 20:42:10 [warning] Something is wrong... []      @...\example1.php(8)
// )

use Stk2k\MemoryLogger\SimpleMemoryLogger;
use Stk2k\MemoryLogger\Formatter\DefaultFormatter;

$logger = new SimpleMemoryLogger(new DefaultFormatter('%LEVEL%:%MESSAGE%'));
$logger->debug('Hello, world!');
$logger->warning('Something is wrong...');

print_r($logger->toArray());
// Array
// (
//     [0] => debug:Hello, world!
//     [1] => warning:Something is wrong...
// )

use Stk2k\MemoryLogger\SimpleMemoryLogger;
use Stk2k\MemoryLogger\LogFormatterInterface;

class JsonFormatter implements LogFormatterInterface
{
    public function format($level, $message, array $context, string $file, int $line): string
    {
        return json_encode([
            'level' => $level, 'message' => $message, 'context' => $context, 'file' => $file, 'line' => $line,
        ]);
    }
}

$logger = new SimpleMemoryLogger(new JsonFormatter);
$logger->debug('Hello, world!');
$logger->warning('Something is wrong...');

print_r($logger->toArray());
// Array
// (
//    [0] => {"level":"debug","message":"Hello, world!","context":[],"file":".../example3.php","line":18}
//    [1] => {"level":"warning","message":"Something is wrong...","context":[],"file":".../example3.php","line":19}
// )

use Stk2k\MemoryLogger\SimpleMemoryLogger;

$logger = new SimpleMemoryLogger();
$logger->debug('Hello, world!');
$logger->warning('Something is wrong...');

$logger->each(function($line){
    echo $line . PHP_EOL;
});
// 2019-10-11 20:58:33 [debug] Hello, world! []      @.../example4.php(7)
// 2019-10-11 20:58:33 [warning] Something is wrong... []      @.../example4.php(8)

use Stk2k\StreamLogger\BufferedOutputLogger;
use Psr\Log\LogLevel;

$logger = new BufferedOutputLogger();

$logger->debug('debug');
$logger->warning('warning');
$logger->error('error');
$logger->notice('notice');
$logger->info('info');
$logger->critical('critical');

$logger->filterByLevelBetween(LogLevel::WARNING, LogLevel::ERROR)->flush();
// 2019-10-12 10:46:29 [warning] warning []      @.../xample6.php(10)
// 2019-10-12 10:46:29 [error] error []      @.../example6.php(11)

use Psr\Log\LogLevel;
use Stk2k\StreamLogger\BufferedOutputLogger;
use Stk2k\StreamLogger\LogMessage;

$logger = new BufferedOutputLogger();

$logger->debug('debug');
$logger->warning('warning');
$logger->error('error');
$logger->notice('notice');
$logger->info('info');
$logger->critical('critical');

$logger->filter(function(LogMessage $m){
    return $m->getLevel() === LogLevel::INFO || $m->getLevel() === LogLevel::WARNING;
})->flush();
// 2019-10-12 14:09:04 [warning] warning []      @.../example11.php(11)
// 2019-10-12 14:09:04 [info] info []      @.../example11.php(14)