stk2k/stream-logger

PSR logger using php://stream stream

0.1.0 2019-10-12 08:49 UTC

This package is auto-updated.

Last update: 2024-11-12 23:38:40 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

PSR-3 logger compliant stream logger

Feature

  • PSR/Log compliant.
  • PHP output streams
    • php://stdout
    • php://stderr
    • php://output
    • php://memory
    • php://temp
  • Default formatter
  • Custom format with default formatter
  • Custom formatter class
  • toArray()/each() method to get all log messages(only for MemoryLogger).
  • filter methods for buffered stream loggers.

Usage

Default format

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)
// )

Custom format with default formatter

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...
// )

Custom formatter class

You can also define your own formatter by implementing LogFormatterInterface.

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}
// )

Retrieving each lines

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)

Filtering by log level

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)

Filtering by callback

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)

Requirement

PHP 7.0 or later

Installing stk2k/stream-logger

The recommended way to install stk2k/stream-logger is through Composer.

composer require stk2k/stream-logger

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

This library is licensed under the MIT license.

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.