-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3db875e
commit b0e5c8b
Showing
6 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace yii1tech\psr\log; | ||
|
||
use CLogger; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LoggerTrait; | ||
use Yii; | ||
|
||
/** | ||
* PsrLogger is a wrapper around Yii standard {@see \CLogger}, which provides PSR compatible interface. | ||
* | ||
* This class can be used in case you work with 3rd party library, which requires PSR Log component to be passed into. | ||
* | ||
* AbstractArrayLogger is an intermediate class for {@see PsrLogger} creation. | ||
* Its existence required since {@see \Psr\Log\LoggerInterface} changes signature over different PHP versions. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
abstract class AbstractPsrLogger implements LoggerInterface | ||
{ | ||
use LoggerTrait; | ||
|
||
/** | ||
* @var \CLogger|null Yii logger to write logs into. | ||
*/ | ||
private $_yiiLogger; | ||
|
||
/** | ||
* @return \CLogger Yii logger instance. | ||
*/ | ||
public function getYiiLogger(): CLogger | ||
{ | ||
if ($this->_yiiLogger === null) { | ||
$this->_yiiLogger = Yii::getLogger(); | ||
} | ||
|
||
return $this->_yiiLogger; | ||
} | ||
|
||
/** | ||
* @param \CLogger|null $yiiLogger Yii logger instance to be used. | ||
* @return static self reference. | ||
*/ | ||
public function setYiiLogger(?CLogger $yiiLogger): self | ||
{ | ||
$this->_yiiLogger = $yiiLogger; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Logs with an arbitrary level. | ||
* This method should be invoked during {@see log()} method implementation. | ||
* | ||
* @param mixed $level log level. | ||
* @param string|\Stringable $message log message. | ||
* @param array $context log context. | ||
* @return void | ||
*/ | ||
protected function writeLog($level, $message, array $context = []): void | ||
{ | ||
$yiiLogger = $this->getYiiLogger(); | ||
|
||
if (!$yiiLogger instanceof Logger) { | ||
$context = $context['category'] ?? 'application'; | ||
} | ||
|
||
$yiiLogger->log( | ||
$message, | ||
LogLevelConverter::toYii($level), | ||
$context | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,10 @@ | |
use Yii; | ||
|
||
/** | ||
* Logger is an enhanced version of Yii standard {@see \CLogger}, which allows passing messages to the wrapped PSR logger. | ||
* | ||
* 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. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace yii1tech\psr\log; | ||
|
||
if (version_compare(phpversion(), '8.0', '>=')) { | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
class PsrLogger extends AbstractPsrLogger | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function log($level, string|\Stringable $message, array $context = []): void | ||
{ | ||
$this->writeLog($level, $message, $context); | ||
} | ||
} | ||
} else { | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
class PsrLogger extends AbstractPsrLogger | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function log($level, $message, array $context = []): void | ||
{ | ||
$this->writeLog($level, $message, $context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace yii1tech\psr\log\test; | ||
|
||
use CLogger; | ||
use Psr\Log\LogLevel; | ||
use Yii; | ||
use yii1tech\psr\log\PsrLogger; | ||
|
||
class PsrLoggerTest extends TestCase | ||
{ | ||
public function testSetupYiiLogger(): void | ||
{ | ||
$psrLogger = new PsrLogger(); | ||
|
||
$yiiLogger = new CLogger(); | ||
|
||
$psrLogger->setYiiLogger($yiiLogger); | ||
|
||
$this->assertSame($yiiLogger, $psrLogger->getYiiLogger()); | ||
} | ||
|
||
public function testGetDefaultYiiLogger(): void | ||
{ | ||
$psrLogger = new PsrLogger(); | ||
|
||
$yiiLogger = $psrLogger->getYiiLogger(); | ||
|
||
$this->assertTrue($yiiLogger instanceof CLogger); | ||
$this->assertSame($yiiLogger, Yii::getLogger()); | ||
} | ||
|
||
public function testWriteLog(): void | ||
{ | ||
$yiiLogger = new CLogger(); | ||
|
||
$logger = (new PsrLogger()) | ||
->setYiiLogger($yiiLogger); | ||
|
||
$logger->log(LogLevel::INFO, 'test message', ['category' => 'context-category']); | ||
|
||
$logs = $yiiLogger->getLogs(); | ||
$yiiLogger->flush(); | ||
|
||
$this->assertFalse(empty($logs[0])); | ||
$this->assertSame('test message', $logs[0][0]); | ||
$this->assertSame(LogLevel::INFO, $logs[0][1]); | ||
$this->assertSame('context-category', $logs[0][2]); | ||
|
||
$logger->log(LogLevel::INFO, 'test message', ['foo' => 'bar']); | ||
|
||
$logs = $yiiLogger->getLogs(); | ||
$yiiLogger->flush(); | ||
|
||
$this->assertFalse(empty($logs[0])); | ||
$this->assertSame('application', $logs[0][2]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters