Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update psr/log to v2 and fix Logger interface #7967

Merged
merged 7 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion admin/framework/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"laminas/laminas-escaper": "^2.9",
"psr/log": "^1.1"
"psr/log": "^2.0"
},
"require-dev": {
"codeigniter/coding-standard": "^1.5",
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"laminas/laminas-escaper": "^2.9",
"psr/log": "^1.1"
"psr/log": "^2.0"
},
"require-dev": {
"codeigniter/coding-standard": "^1.5",
Expand Down
8 changes: 5 additions & 3 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ function lang(string $line, array $args = [], ?string $locale = null)
* - info
* - debug
*
* @return bool
* @return void
*/
function log_message(string $level, string $message, array $context = [])
{
Expand All @@ -804,10 +804,12 @@ function log_message(string $level, string $message, array $context = [])
if (ENVIRONMENT === 'testing') {
$logger = new TestLogger(new Logger());

return $logger->log($level, $message, $context);
$logger->log($level, $message, $context);

return;
}

return Services::logger(true)->log($level, $message, $context); // @codeCoverageIgnore
Services::logger(true)->log($level, $message, $context); // @codeCoverageIgnore
}
}

Expand Down
3 changes: 1 addition & 2 deletions system/ComposerScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class ComposerScripts
],
'psr-log' => [
'license' => __DIR__ . '/../vendor/psr/log/LICENSE',
'from' => __DIR__ . '/../vendor/psr/log/Psr/Log/',
'from' => __DIR__ . '/../vendor/psr/log/src/',
'to' => __DIR__ . '/ThirdParty/PSR/Log/',
],
];
Expand Down Expand Up @@ -84,7 +84,6 @@ public static function postUpdate()
}

self::copyKintInitFiles();
self::recursiveDelete(self::$dependencies['psr-log']['to'] . 'Test/');
}

/**
Expand Down
39 changes: 19 additions & 20 deletions system/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Log\Handlers\HandlerInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Stringable;
use Throwable;

/**
Expand Down Expand Up @@ -157,9 +158,9 @@ public function __construct($config, bool $debug = CI_DEBUG)
*
* @param string $message
*/
public function emergency($message, array $context = []): bool
public function emergency(string|Stringable $message, array $context = []): void
{
return $this->log('emergency', $message, $context);
$this->log('emergency', $message, $context);
}

/**
Expand All @@ -170,9 +171,9 @@ public function emergency($message, array $context = []): bool
*
* @param string $message
*/
public function alert($message, array $context = []): bool
public function alert(string|Stringable $message, array $context = []): void
{
return $this->log('alert', $message, $context);
$this->log('alert', $message, $context);
}

/**
Expand All @@ -182,9 +183,9 @@ public function alert($message, array $context = []): bool
*
* @param string $message
*/
public function critical($message, array $context = []): bool
public function critical(string|Stringable $message, array $context = []): void
{
return $this->log('critical', $message, $context);
$this->log('critical', $message, $context);
}

/**
Expand All @@ -193,9 +194,9 @@ public function critical($message, array $context = []): bool
*
* @param string $message
*/
public function error($message, array $context = []): bool
public function error(string|Stringable $message, array $context = []): void
{
return $this->log('error', $message, $context);
$this->log('error', $message, $context);
}

/**
Expand All @@ -206,19 +207,19 @@ public function error($message, array $context = []): bool
*
* @param string $message
*/
public function warning($message, array $context = []): bool
public function warning(string|Stringable $message, array $context = []): void
{
return $this->log('warning', $message, $context);
$this->log('warning', $message, $context);
}

/**
* Normal but significant events.
*
* @param string $message
*/
public function notice($message, array $context = []): bool
public function notice(string|Stringable $message, array $context = []): void
{
return $this->log('notice', $message, $context);
$this->log('notice', $message, $context);
}

/**
Expand All @@ -228,19 +229,19 @@ public function notice($message, array $context = []): bool
*
* @param string $message
*/
public function info($message, array $context = []): bool
public function info(string|Stringable $message, array $context = []): void
{
return $this->log('info', $message, $context);
$this->log('info', $message, $context);
}

/**
* Detailed debug information.
*
* @param string $message
*/
public function debug($message, array $context = []): bool
public function debug(string|Stringable $message, array $context = []): void
{
return $this->log('debug', $message, $context);
$this->log('debug', $message, $context);
}

/**
Expand All @@ -249,7 +250,7 @@ public function debug($message, array $context = []): bool
* @param string $level
* @param string $message
*/
public function log($level, $message, array $context = []): bool
public function log($level, string|Stringable $message, array $context = []): void
{
if (is_numeric($level)) {
$level = array_search((int) $level, $this->logLevels, true);
Expand All @@ -262,7 +263,7 @@ public function log($level, $message, array $context = []): bool

// Does the app want to log this right now?
if (! in_array($level, $this->loggableLevels, true)) {
return false;
return;
}

// Parse our placeholders
Expand Down Expand Up @@ -295,8 +296,6 @@ public function log($level, $message, array $context = []): bool
break;
}
}

return true;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions system/Test/TestLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Test;

use CodeIgniter\Log\Logger;
use Stringable;

/**
* @see \CodeIgniter\Test\TestLoggerTest
Expand All @@ -27,7 +28,7 @@ class TestLogger extends Logger
* @param string $level
* @param string $message
*/
public function log($level, $message, array $context = []): bool
public function log($level, string|Stringable $message, array $context = []): void
{
// While this requires duplicate work, we want to ensure
// we have the final message to test against.
Expand All @@ -52,7 +53,7 @@ public function log($level, $message, array $context = []): bool
];

// Let the parent do it's thing.
return parent::log($level, $message, $context);
parent::log($level, $message, $context);
}

/**
Expand Down
115 changes: 1 addition & 114 deletions system/ThirdParty/PSR/Log/AbstractLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,118 +11,5 @@
*/
abstract class AbstractLogger implements LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function emergency($message, array $context = array())
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function alert($message, array $context = array())
{
$this->log(LogLevel::ALERT, $message, $context);
}

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function critical($message, array $context = array())
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function error($message, array $context = array())
{
$this->log(LogLevel::ERROR, $message, $context);
}

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function warning($message, array $context = array())
{
$this->log(LogLevel::WARNING, $message, $context);
}

/**
* Normal but significant events.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function notice($message, array $context = array())
{
$this->log(LogLevel::NOTICE, $message, $context);
}

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function info($message, array $context = array())
{
$this->log(LogLevel::INFO, $message, $context);
}

/**
* Detailed debug information.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function debug($message, array $context = array())
{
$this->log(LogLevel::DEBUG, $message, $context);
}
use LoggerTrait;
}
2 changes: 1 addition & 1 deletion system/ThirdParty/PSR/Log/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait LoggerAwareTrait
*
* @var LoggerInterface|null
*/
protected $logger;
protected ?LoggerInterface $logger = null;

/**
* Sets a logger.
Expand Down
Loading
Loading