Skip to content

Commit

Permalink
fixed compatibility with "psr/log" 1.x at PHP 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 28, 2023
1 parent 9360c58 commit 89d460a
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 244 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Yii1 PSR Log extension
======================

1.0.2 Under Development
-----------------------

- Bug: Fixed compatibility with "psr/log" 1.x at PHP 8.x (klimov-paul)


1.0.2, July 20, 2023
--------------------

Expand Down
123 changes: 0 additions & 123 deletions src/AbstractPsrLogger.php

This file was deleted.

112 changes: 106 additions & 6 deletions src/PsrLogger.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,114 @@
<?php

namespace yii1tech\psr\log;

use CLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Throwable;
use Yii;

/**
* Splits actual class declaration into 2 separated branches, allowing support both PHP 8.x and PHP 7.x.
* This is necessary since {@see \Psr\Log\LoggerInterface} changes signature over different PHP versions.
* 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.
*
* Application configuration example:
*
* ```php
* return [
* 'components' => [
* \Psr\Log\LoggerInterface::class => [
* 'class' => \yii1tech\psr\log\PsrLogger::class,
* ],
* // ...
* ],
* // ...
* ];
* ```
*
* > Note: in order to handle log context properly this class should be used in junction with {@see \yii1tech\psr\log\Logger}
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class PsrLogger implements LoggerInterface
{
use LoggerTrait;
use HasGlobalContext;

/**
* @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;
}

/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = []): void
{
$context = array_merge(
$this->resolveGlobalContext(),
$context
);

$yiiLogger = $this->getYiiLogger();

if (!$yiiLogger instanceof Logger) {
$context = $context['category'] ?? 'application';
}

$yiiLogger->log(
$message,
LogLevelConverter::toYii($level),
$context
);
}

/**
* {@inheritdoc}
*/
protected function logGlobalContextResolutionError(Throwable $exception): void
{
$errorMessage = 'Unable to resolve global log context: ' . $exception->getMessage();

$this->getYiiLogger()->log($errorMessage, CLogger::LEVEL_ERROR, 'system.log');
}

if (version_compare(phpversion(), '8.0', '>=')) {
require __DIR__ . '/compatibility/PsrLogger.v8.php';
} else {
require __DIR__ . '/compatibility/PsrLogger.v7.php';
/**
* Creates new self instance.
* This method can be useful when writing chain methods calls.
*
* @since 1.0.1
*
* @param mixed ...$args constructor arguments.
* @return static new self instance.
*/
public static function new(...$args): self
{
return new static(...$args);
}
}
17 changes: 0 additions & 17 deletions src/compatibility/PsrLogger.v7.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/compatibility/PsrLogger.v8.php

This file was deleted.

39 changes: 0 additions & 39 deletions tests/support/AbstractArrayLogger.php

This file was deleted.

38 changes: 34 additions & 4 deletions tests/support/ArrayLogger.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
<?php

if (version_compare(phpversion(), '8.0', '>=')) {
require __DIR__ . '/compatibility/ArrayLogger.v8.php';
} else {
require __DIR__ . '/compatibility/ArrayLogger.v7.php';
namespace yii1tech\psr\log\test\support;

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

class ArrayLogger implements LoggerInterface
{
use LoggerTrait;

/**
* @var array[] written log entries.
*/
public $logs = [];

/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = []): void
{
$this->logs[] = [
'level' => $level,
'message' => $message,
'context' => $context,
];
}

public function flush(): array
{
$logs = $this->logs;

$this->logs = [];

return $logs;
}
}
Loading

0 comments on commit 89d460a

Please sign in to comment.