Skip to content

Commit

Permalink
create PsrLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 18, 2023
1 parent 3db875e commit b0e5c8b
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 3 deletions.
76 changes: 76 additions & 0 deletions src/AbstractPsrLogger.php
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
);
}
}
4 changes: 4 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand Down
33 changes: 33 additions & 0 deletions src/PsrLogger.php
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);
}
}
}
58 changes: 58 additions & 0 deletions tests/PsrLoggerTest.php
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]);
}
}
2 changes: 1 addition & 1 deletion tests/support/AbstractArrayLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class AbstractArrayLogger implements LoggerInterface
*/
public $logs = [];

protected function baseLog($level, $message, array $context = []): void
protected function writeLog($level, $message, array $context = []): void
{
$this->logs[] = [
'level' => $level,
Expand Down
4 changes: 2 additions & 2 deletions tests/support/ArrayLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class ArrayLogger extends AbstractArrayLogger
{
public function log($level, string|\Stringable $message, array $context = []): void
{
$this->baseLog($level, $message, $context);
$this->writeLog($level, $message, $context);
}
}
} else {
class ArrayLogger extends AbstractArrayLogger
{
public function log($level, $message, array $context = []): void
{
$this->baseLog($level, $message, $context);
$this->writeLog($level, $message, $context);
}
}
}

0 comments on commit b0e5c8b

Please sign in to comment.