mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-23 09:36:11 +02:00
Add Record/Level/LevelName type aliases and improve phpstan type coverage to level 6
This commit is contained in:
@@ -15,6 +15,7 @@ use DateTimeZone;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LogLevel;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -24,6 +25,10 @@ use Throwable;
|
||||
* and uses them to store records that are added to it.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* @phpstan-type Level Logger::DEBUG|Logger::INFO|Logger::NOTICE|Logger::WARNING|Logger::ERROR|Logger::CRITICAL|Logger::ALERT|Logger::EMERGENCY
|
||||
* @phpstan-type LevelName 'DEBUG'|'INFO'|'NOTICE'|'WARNING'|'ERROR'|'CRITICAL'|'ALERT'|'EMERGENCY'
|
||||
* @phpstan-type Record array{message: string, context: mixed[], level: Level, level_name: LevelName, channel: string, datetime: \DateTimeImmutable, extra: mixed[]}
|
||||
*/
|
||||
class Logger implements LoggerInterface, ResettableInterface
|
||||
{
|
||||
@@ -91,6 +96,8 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
* This is a static variable and not a constant to serve as an extension point for custom levels
|
||||
*
|
||||
* @var array<int, string> $levels Logging levels with the levels as key
|
||||
*
|
||||
* @phpstan-var array<Level, LevelName> $levels Logging levels with the levels as key
|
||||
*/
|
||||
protected static $levels = [
|
||||
self::DEBUG => 'DEBUG',
|
||||
@@ -276,6 +283,8 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
* @param string $message The log message
|
||||
* @param mixed[] $context The log context
|
||||
* @return bool Whether the record has been processed
|
||||
*
|
||||
* @phpstan-param Level $level
|
||||
*/
|
||||
public function addRecord(int $level, string $message, array $context = []): bool
|
||||
{
|
||||
@@ -383,6 +392,9 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
* Gets the name of the logging level.
|
||||
*
|
||||
* @throws \Psr\Log\InvalidArgumentException If level is not defined
|
||||
*
|
||||
* @phpstan-param Level $level
|
||||
* @phpstan-return LevelName
|
||||
*/
|
||||
public static function getLevelName(int $level): string
|
||||
{
|
||||
@@ -398,11 +410,15 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
*
|
||||
* @param string|int $level Level number (monolog) or name (PSR-3)
|
||||
* @throws \Psr\Log\InvalidArgumentException If level is not defined
|
||||
*
|
||||
* @phpstan-param Level|LevelName|LogLevel::* $level
|
||||
* @phpstan-return Level
|
||||
*/
|
||||
public static function toMonologLevel($level): int
|
||||
{
|
||||
if (is_string($level)) {
|
||||
if (is_numeric($level)) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
return intval($level);
|
||||
}
|
||||
|
||||
@@ -413,18 +429,21 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
return constant(__CLASS__ . '::' . $upper);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
|
||||
throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels) + static::$levels));
|
||||
}
|
||||
|
||||
if (!is_int($level)) {
|
||||
throw new InvalidArgumentException('Level "'.var_export($level, true).'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
|
||||
throw new InvalidArgumentException('Level "'.var_export($level, true).'" is not defined, use one of: '.implode(', ', array_keys(static::$levels) + static::$levels));
|
||||
}
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
return $level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the Logger has a handler that listens on the given level
|
||||
*
|
||||
* @phpstan-param Level $level
|
||||
*/
|
||||
public function isHandling(int $level): bool
|
||||
{
|
||||
@@ -463,9 +482,11 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
*
|
||||
* This method allows for compatibility with common interfaces.
|
||||
*
|
||||
* @param mixed $level The log level
|
||||
* @param string $message The log message
|
||||
* @param mixed[] $context The log context
|
||||
* @param int|string $level The log level
|
||||
* @param string $message The log message
|
||||
* @param mixed[] $context The log context
|
||||
*
|
||||
* @phpstan-param Level|LevelName|LogLevel::* $level
|
||||
*/
|
||||
public function log($level, $message, array $context = []): void
|
||||
{
|
||||
@@ -599,6 +620,9 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
/**
|
||||
* Delegates exception management to the custom exception handler,
|
||||
* or throws the exception if no custom handler is set.
|
||||
*
|
||||
* @param array $record
|
||||
* @phpstan-param Record $record
|
||||
*/
|
||||
protected function handleException(Throwable $e, array $record): void
|
||||
{
|
||||
|
Reference in New Issue
Block a user