From f805fa4169a113f71208cc61908f70016919af57 Mon Sep 17 00:00:00 2001 From: roquie Date: Tue, 11 Oct 2022 09:42:06 +0300 Subject: [PATCH] 1. Update phpunit schema to new 2. Moving to PHP 8.1 minimal version 3. Add stderr support --- LICENSE | 2 +- README.md | 6 ++-- composer.json | 6 ++-- phpunit.xml | 41 ++++++++++----------------- src/Logger.php | 66 +++++++++++++++++++++++++++++--------------- tests/LoggerTest.php | 15 +++++++--- 6 files changed, 77 insertions(+), 59 deletions(-) diff --git a/LICENSE b/LICENSE index cd948e6..1b25029 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License -Copyright © 2020 spacetab.io, Inc. https://spacetab.io +Copyright © 2022 spacetab.io, Inc. https://spacetab.io Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index e1eb85e..0739d94 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Since `2.0.0` logger is non-blocking and based on Amp and Monolog. I create this library with one target, — I'm sick of always copies and paste same code to any of my microservices with logs. -This wrapper solves one problem with logs. And it name, — fucking brackets -> [] [] [] []. +This wrapper solves one problem with logs. And this problem, — fucking brackets -> [] [] [] []. Example, how this wrapper present the logs: @@ -79,7 +79,7 @@ $log->register(); $log->getMonolog()->info('Hello world'); ``` -4) Okay. It's cool. But I have write logs to multiple streams. How I do it? +4) Okay. It's cool. But I have written logs to multiple streams. How I do it? ```php =7.4", + "php": ">=8.1", "amphp/log": "^1.1" }, "require-dev": { - "symfony/var-dumper": "^4.2 || ^5.0", - "phpunit/phpunit": "^9.1", + "symfony/var-dumper": "^4 || ^5 || ^6", + "phpunit/phpunit": "^9", "phpstan/phpstan": "^0.12" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index fc0007e..ceb5900 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,28 +1,17 @@ - - - - ./tests - - - - - ./src - - ./tests - ./vendor - - - + + + + ./src + + + ./tests + ./vendor + + + + + ./tests + + diff --git a/src/Logger.php b/src/Logger.php index e8920d4..c7ecbea 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -1,4 +1,6 @@ -channel = $channel; $this->level = $level; $this->monolog = new Monolog($channel); @@ -54,6 +44,7 @@ public function __construct(string $channel = self::CHANNEL, string $level = Log * * @param string $channel * @param string $level + * * @return Logger */ public static function new(string $channel = self::CHANNEL, string $level = LogLevel::INFO): Logger @@ -65,13 +56,27 @@ public static function new(string $channel = self::CHANNEL, string $level = LogL } /** - * Return registered default logger. + * Return registered default logger (forward logs to stdout). * * @param string $channel * @param string $level - * @return \Psr\Log\LoggerInterface + * + * @return LoggerInterface */ public static function default(string $channel = self::CHANNEL, string $level = LogLevel::INFO): LoggerInterface + { + return self::stdout($channel, $level); + } + + /** + * Return registered logger which forward logs to stderr. + * + * @param string $channel + * @param string $level + * + * @return LoggerInterface + */ + public static function stdout(string $channel = self::CHANNEL, string $level = LogLevel::INFO): LoggerInterface { $log = new Logger($channel, $level); $log->addStreamHandler(); @@ -80,10 +85,28 @@ public static function default(string $channel = self::CHANNEL, string $level = return $log->getMonolog(); } + /** + * Return registered logger which forward logs to stderr. + * + * @param string $channel + * @param string $level + * + * @return LoggerInterface + */ + public static function stderr(string $channel = self::CHANNEL, string $level = LogLevel::INFO): LoggerInterface + { + $log = new Logger($channel, $level); + $log->addStreamHandler(ByteStream\getStderr()); + $log->register(); + + return $log->getMonolog(); + } + /** * Add Monolog handler use callback. * * @param callable $callback + * * @return Logger */ public function addHandler(callable $callback): self @@ -109,22 +132,21 @@ public function register(): void * Create Monolog logger without fucking brackets -> [] [] [] [] [] [] [] [] [] [] * if context and extra is empty. */ - public function addStreamHandler(): void + public function addStreamHandler(?ByteStream\OutputStream $outputStream = null): void { - $this->addHandler(function (string $level) { + $outputStream = $outputStream ?: ByteStream\getStdout(); + + $this->addHandler(function (string $level) use ($outputStream) { $formatter = new ConsoleFormatter(); $formatter->ignoreEmptyContextAndExtra(); - $handler = new StreamHandler(ByteStream\getStdout(), $level); + $handler = new StreamHandler($outputStream, $level); $handler->setFormatter($formatter); return $handler; }); } - /** - * @return Monolog - */ public function getMonolog(): Monolog { return $this->monolog; diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 68a38ab..1b0117d 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -1,4 +1,6 @@ -addStreamHandler(); $log->addHandler(function (): HandlerInterface { - return new class() extends AbstractHandler implements HandlerInterface { + return new class() extends AbstractHandler { public function handle(array $record): bool { global $globalMustDieOrNot; $globalMustDieOrNot[] = $record; @@ -35,7 +37,7 @@ public function handle(array $record): bool { } } - public function testHowWorksStaticMethods() + public function testHowWorksStaticMethods(): void { $log = Logger::default(); @@ -46,5 +48,10 @@ public function testHowWorksStaticMethods() $log = Logger::new(); $this->assertInstanceOf(Logger::class, $log); + + $log = Logger::stderr(); + + $this->assertInstanceOf(\Monolog\Logger::class, $log); + $this->assertInstanceOf(LoggerInterface::class, $log); } }