1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-21 08:36:33 +02:00

Remove LevelName enum in favor of a Level::getName method, fixes #1667 (#1668)

This commit is contained in:
Jordi Boggiano
2022-05-10 11:15:16 +02:00
committed by GitHub
parent d381140ca1
commit 1dacc790b9
65 changed files with 220 additions and 280 deletions

View File

@@ -14,7 +14,19 @@ namespace Monolog;
use Psr\Log\LogLevel;
/**
* @see LevelName
* Represents the log levels
*
* Monolog supports the logging levels described by RFC 5424 {@see https://datatracker.ietf.org/doc/html/rfc5424}
* but due to BC the severity values used internally are not 0-7.
*
* To get the level name out of a Level there are three options:
*
* - Use ->getName() to get the standard Monolog name which is full uppercased (e.g. "DEBUG")
* - Use ->toPsrLogLevel() to get the standard PSR-3 name which is full lowercased (e.g. "debug")
* - Use ->name to get the enum case's name which is capitalized (e.g. "Debug")
*
* To get the value for filtering, if the includes/isLowerThan/isHigherThan methods are
* not enough, you can use ->value to get the enum case's integer value.
*/
enum Level: int
{
@@ -68,20 +80,33 @@ enum Level: int
*/
case Emergency = 600;
public static function fromLevelName(LevelName $name): self
/**
* @param value-of<self::NAMES>|LogLevel::*|'Debug'|'Info'|'Notice'|'Warning'|'Error'|'Critical'|'Alert'|'Emergency' $name
* @return static
*/
public static function fromName(string $name): self
{
return match ($name) {
LevelName::Debug => self::Debug,
LevelName::Info => self::Info,
LevelName::Notice => self::Notice,
LevelName::Warning => self::Warning,
LevelName::Error => self::Error,
LevelName::Critical => self::Critical,
LevelName::Alert => self::Alert,
LevelName::Emergency => self::Emergency,
'debug', 'Debug', 'DEBUG' => self::Debug,
'info', 'Info', 'INFO' => self::Info,
'notice', 'Notice', 'NOTICE' => self::Notice,
'warning', 'Warning', 'WARNING' => self::Warning,
'error', 'Error', 'ERROR' => self::Error,
'critical', 'Critical', 'CRITICAL' => self::Critical,
'alert', 'Alert', 'ALERT' => self::Alert,
'emergency', 'Emergency', 'EMERGENCY' => self::Emergency,
};
}
/**
* @param value-of<self::VALUES> $value
* @return static
*/
public static function fromValue(int $value): self
{
return self::from($value);
}
/**
* Returns true if the passed $level is higher or equal to $this
*/
@@ -100,9 +125,25 @@ enum Level: int
return $this->value < $level->value;
}
public function toLevelName(): LevelName
/**
* Returns the monolog standardized all-capitals name of the level
*
* Use this instead of $level->name which returns the enum case name (e.g. Debug vs DEBUG if you use getName())
*
* @return value-of<self::NAMES>
*/
public function getName(): string
{
return LevelName::fromLevel($this);
return match ($this) {
self::Debug => 'DEBUG',
self::Info => 'INFO',
self::Notice => 'NOTICE',
self::Warning => 'WARNING',
self::Error => 'ERROR',
self::Critical => 'CRITICAL',
self::Alert => 'ALERT',
self::Emergency => 'EMERGENCY',
};
}
/**
@@ -132,4 +173,15 @@ enum Level: int
550,
600,
];
public const NAMES = [
'DEBUG',
'INFO',
'NOTICE',
'WARNING',
'ERROR',
'CRITICAL',
'ALERT',
'EMERGENCY',
];
}