diff --git a/CHANGELOG.md b/CHANGELOG.md index 6503532..c3adc34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 -------------------- diff --git a/src/AbstractPsrLogger.php b/src/AbstractPsrLogger.php deleted file mode 100644 index 0953374..0000000 --- a/src/AbstractPsrLogger.php +++ /dev/null @@ -1,123 +0,0 @@ - [ - * \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 - * @since 1.0 - */ -abstract class AbstractPsrLogger 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; - } - - /** - * 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 - { - $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'); - } - - /** - * 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); - } -} \ No newline at end of file diff --git a/src/PsrLogger.php b/src/PsrLogger.php index c00df85..e6ea5e5 100644 --- a/src/PsrLogger.php +++ b/src/PsrLogger.php @@ -1,14 +1,114 @@ [ + * \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 * @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); + } } \ No newline at end of file diff --git a/src/compatibility/PsrLogger.v7.php b/src/compatibility/PsrLogger.v7.php deleted file mode 100644 index 2412e6d..0000000 --- a/src/compatibility/PsrLogger.v7.php +++ /dev/null @@ -1,17 +0,0 @@ -writeLog($level, $message, $context); - } -} \ No newline at end of file diff --git a/src/compatibility/PsrLogger.v8.php b/src/compatibility/PsrLogger.v8.php deleted file mode 100644 index 22c1ed9..0000000 --- a/src/compatibility/PsrLogger.v8.php +++ /dev/null @@ -1,17 +0,0 @@ -writeLog($level, $message, $context); - } -} \ No newline at end of file diff --git a/tests/support/AbstractArrayLogger.php b/tests/support/AbstractArrayLogger.php deleted file mode 100644 index afef026..0000000 --- a/tests/support/AbstractArrayLogger.php +++ /dev/null @@ -1,39 +0,0 @@ -logs[] = [ - 'level' => $level, - 'message' => $message, - 'context' => $context, - ]; - } - - public function flush(): array - { - $logs = $this->logs; - - $this->logs = []; - - return $logs; - } -} \ No newline at end of file diff --git a/tests/support/ArrayLogger.php b/tests/support/ArrayLogger.php index 35bc0ad..2d167d0 100644 --- a/tests/support/ArrayLogger.php +++ b/tests/support/ArrayLogger.php @@ -1,7 +1,37 @@ =')) { - 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; + } } \ No newline at end of file diff --git a/tests/support/compatibility/ArrayLogger.v7.php b/tests/support/compatibility/ArrayLogger.v7.php deleted file mode 100644 index 2783363..0000000 --- a/tests/support/compatibility/ArrayLogger.v7.php +++ /dev/null @@ -1,19 +0,0 @@ -writeLog($level, $message, $context); - } -} \ No newline at end of file diff --git a/tests/support/compatibility/ArrayLogger.v8.php b/tests/support/compatibility/ArrayLogger.v8.php deleted file mode 100644 index 2684247..0000000 --- a/tests/support/compatibility/ArrayLogger.v8.php +++ /dev/null @@ -1,19 +0,0 @@ -writeLog($level, $message, $context); - } -} \ No newline at end of file