Skip to content

Commit

Permalink
add ability to setup PSR logger as Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 18, 2023
1 parent 1dd4290 commit 5e563a5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
40 changes: 38 additions & 2 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @since 1.0
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
32 changes: 30 additions & 2 deletions src/PsrLogRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @since 1.0
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/PsrLogRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e563a5

Please sign in to comment.