Skip to content

Commit

Permalink
fix: incorrect Logger interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 24, 2023
1 parent ec661ef commit ee8887d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
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
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

0 comments on commit ee8887d

Please sign in to comment.