-
-
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.
fixed compatibility with "psr/log" 1.x at PHP 8.x
- Loading branch information
1 parent
9360c58
commit 89d460a
Showing
9 changed files
with
146 additions
and
244 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.