From 5e563a54450b5d6c792074267008a5bbec87b3be Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Tue, 18 Jul 2023 16:51:45 +0300 Subject: [PATCH] add ability to setup PSR logger as `Closure` --- src/Logger.php | 40 +++++++++++++++++++++++++++++++++++++-- src/PsrLogRoute.php | 32 +++++++++++++++++++++++++++++-- tests/LoggerTest.php | 6 ++++++ tests/PsrLogRouteTest.php | 6 ++++++ 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index e5fffa9..4e4a458 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -11,7 +11,27 @@ * * This class can be used in case you with to utilize 3rd party PSR logger library like "Monolog" in your Yii application. * - * @property \Psr\Log\LoggerInterface|string|array|null $psrLogger related PSR logger. + * Configuration example: + * + * ```php + * require __DIR__ . '../vendor/autoload.php'; + * // ... + * + * $app = Yii::createWebApplication($config); + * $app->setLogger( + * \yii1tech\psr\log\Logger::new() + * ->setPsrLogger(function () { + * $log = new \Monolog\Logger('yii'); + * $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning)); + * + * return $log; + * }) + * ->enableYiiLog(true) + * ); + * $app->run(); + * ``` + * + * @property \Psr\Log\LoggerInterface|\Closure|string|array|null $psrLogger related PSR logger. * * @author Paul Klimov * @since 1.0 @@ -39,13 +59,18 @@ public function getPsrLogger(): ?LoggerInterface if (!is_object($this->_psrLogger)) { $this->_psrLogger = Yii::createComponent($this->_psrLogger); + } elseif ($this->_psrLogger instanceof \Closure) { + $this->_psrLogger = call_user_func($this->_psrLogger); } return $this->_psrLogger; } /** - * @param \Psr\Log\LoggerInterface|string|array|null $psrLogger + * Sets the PSR logger to pass logs to. + * If `null` provided - no PSR logger will be used. + * + * @param \Psr\Log\LoggerInterface|\Closure|array|string|null $psrLogger related PSR logger. * @return static self reference. */ public function setPsrLogger($psrLogger): self @@ -104,4 +129,15 @@ public function log($message, $level = 'info', $category = 'application'): void ); } } + + /** + * Creates new self instance. + * This method can be useful when writing chain methods calls. + * + * @return static new self instance. + */ + public static function new(): self + { + return new static(); + } } \ No newline at end of file diff --git a/src/PsrLogRoute.php b/src/PsrLogRoute.php index 1d18eeb..1bd8105 100644 --- a/src/PsrLogRoute.php +++ b/src/PsrLogRoute.php @@ -12,10 +12,36 @@ * * This class can be used in case you with to utilize 3rd party PSR logger library like "Monolog" in your Yii application. * + * Application configuration example: + * + * ```php + * return [ + * 'components' => [ + * 'log' => [ + * 'class' => \CLogRouter::class, + * 'routes' => [ + * [ + * 'class' => \yii1tech\psr\log\PsrLogRoute::class, + * 'psrLogger' => function () { + * $log = new \Monolog\Logger('yii'); + * $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning)); + * + * return $log; + * }, + * ], + * // ... + * ], + * ], + * // ... + * ], + * // ... + * ]; + * ``` + * * > Note: even if you use {@see \yii1tech\psr\log\Logger} as Yii logger, this log route will be unable to handle * passed log context correctly. * - * @property \Psr\Log\LoggerInterface|string|array $psrLogger related PSR logger. + * @property \Psr\Log\LoggerInterface|\Closure|string|array $psrLogger related PSR logger. * * @author Paul Klimov * @since 1.0 @@ -38,13 +64,15 @@ public function getPsrLogger(): LoggerInterface if (!is_object($this->_psrLogger)) { $this->_psrLogger = Yii::createComponent($this->_psrLogger); + } elseif ($this->_psrLogger instanceof \Closure) { + $this->_psrLogger = call_user_func($this->_psrLogger); } return $this->_psrLogger; } /** - * @param \Psr\Log\LoggerInterface|array|string $psrLogger + * @param \Psr\Log\LoggerInterface|\Closure|array|string $psrLogger * @return static self reference. */ public function setPsrLogger($psrLogger): self diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index ddcb544..670c504 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -35,6 +35,12 @@ public function testSetupPsrLogger(): void ]); $this->assertTrue($logger->getPsrLogger() instanceof ArrayLogger); + + $logger->setPsrLogger(function () { + return new ArrayLogger(); + }); + + $this->assertTrue($logger->getPsrLogger() instanceof ArrayLogger); } public function testWritePsrLog(): void diff --git a/tests/PsrLogRouteTest.php b/tests/PsrLogRouteTest.php index 74a676e..f2a4cd4 100644 --- a/tests/PsrLogRouteTest.php +++ b/tests/PsrLogRouteTest.php @@ -24,6 +24,12 @@ public function testSetupPsrLogger(): void ]); $this->assertTrue($logRoute->getPsrLogger() instanceof ArrayLogger); + + $logRoute->setPsrLogger(function () { + return new ArrayLogger(); + }); + + $this->assertTrue($logRoute->getPsrLogger() instanceof ArrayLogger); } public function testWriteLog(): void