|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Monolog package. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Monolog; |
| 13 | + |
| 14 | +use Psr\Log\LogLevel; |
| 15 | + |
| 16 | +/** |
| 17 | + * Represents the log levels |
| 18 | + * |
| 19 | + * Monolog supports the logging levels described by RFC 5424 {@see https://datatracker.ietf.org/doc/html/rfc5424} |
| 20 | + * but due to BC the severity values used internally are not 0-7. |
| 21 | + * |
| 22 | + * To get the level name/value out of a Level there are several options: |
| 23 | + * |
| 24 | + * - Use ->getName() to get the standard Monolog name which is full uppercased (e.g. "DEBUG") |
| 25 | + * - Use ->toPsrLogLevel() to get the standard PSR-3 name which is full lowercased (e.g. "debug") |
| 26 | + * - Use ->toRFC5424Level() to get the standard RFC 5424 value (e.g. 7 for debug, 0 for emergency) |
| 27 | + * - Use ->name to get the enum case's name which is capitalized (e.g. "Debug") |
| 28 | + * |
| 29 | + * To get the internal value for filtering, if the includes/isLowerThan/isHigherThan methods are |
| 30 | + * not enough, you can use ->value to get the enum case's integer value. |
| 31 | + */ |
| 32 | +enum Level: int |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Detailed debug information |
| 36 | + */ |
| 37 | + case Debug = 100; |
| 38 | + |
| 39 | + /** |
| 40 | + * Interesting events |
| 41 | + * |
| 42 | + * Examples: User logs in, SQL logs. |
| 43 | + */ |
| 44 | + case Info = 200; |
| 45 | + |
| 46 | + /** |
| 47 | + * Uncommon events |
| 48 | + */ |
| 49 | + case Notice = 250; |
| 50 | + |
| 51 | + /** |
| 52 | + * Exceptional occurrences that are not errors |
| 53 | + * |
| 54 | + * Examples: Use of deprecated APIs, poor use of an API, |
| 55 | + * undesirable things that are not necessarily wrong. |
| 56 | + */ |
| 57 | + case Warning = 300; |
| 58 | + |
| 59 | + /** |
| 60 | + * Runtime errors |
| 61 | + */ |
| 62 | + case Error = 400; |
| 63 | + |
| 64 | + /** |
| 65 | + * Critical conditions |
| 66 | + * |
| 67 | + * Example: Application component unavailable, unexpected exception. |
| 68 | + */ |
| 69 | + case Critical = 500; |
| 70 | + |
| 71 | + /** |
| 72 | + * Action must be taken immediately |
| 73 | + * |
| 74 | + * Example: Entire website down, database unavailable, etc. |
| 75 | + * This should trigger the SMS alerts and wake you up. |
| 76 | + */ |
| 77 | + case Alert = 550; |
| 78 | + |
| 79 | + /** |
| 80 | + * Urgent alert. |
| 81 | + */ |
| 82 | + case Emergency = 600; |
| 83 | + |
| 84 | + /** |
| 85 | + * @param value-of<self::NAMES>|LogLevel::*|'Debug'|'Info'|'Notice'|'Warning'|'Error'|'Critical'|'Alert'|'Emergency' $name |
| 86 | + * @return static |
| 87 | + */ |
| 88 | + public static function fromName(string $name): self |
| 89 | + { |
| 90 | + return match ($name) { |
| 91 | + 'debug', 'Debug', 'DEBUG' => self::Debug, |
| 92 | + 'info', 'Info', 'INFO' => self::Info, |
| 93 | + 'notice', 'Notice', 'NOTICE' => self::Notice, |
| 94 | + 'warning', 'Warning', 'WARNING' => self::Warning, |
| 95 | + 'error', 'Error', 'ERROR' => self::Error, |
| 96 | + 'critical', 'Critical', 'CRITICAL' => self::Critical, |
| 97 | + 'alert', 'Alert', 'ALERT' => self::Alert, |
| 98 | + 'emergency', 'Emergency', 'EMERGENCY' => self::Emergency, |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param value-of<self::VALUES> $value |
| 104 | + * @return static |
| 105 | + */ |
| 106 | + public static function fromValue(int $value): self |
| 107 | + { |
| 108 | + return self::from($value); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns true if the passed $level is higher or equal to $this |
| 113 | + */ |
| 114 | + public function includes(Level $level): bool |
| 115 | + { |
| 116 | + return $this->value <= $level->value; |
| 117 | + } |
| 118 | + |
| 119 | + public function isHigherThan(Level $level): bool |
| 120 | + { |
| 121 | + return $this->value > $level->value; |
| 122 | + } |
| 123 | + |
| 124 | + public function isLowerThan(Level $level): bool |
| 125 | + { |
| 126 | + return $this->value < $level->value; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the monolog standardized all-capitals name of the level |
| 131 | + * |
| 132 | + * Use this instead of $level->name which returns the enum case name (e.g. Debug vs DEBUG if you use getName()) |
| 133 | + * |
| 134 | + * @return value-of<self::NAMES> |
| 135 | + */ |
| 136 | + public function getName(): string |
| 137 | + { |
| 138 | + return match ($this) { |
| 139 | + self::Debug => 'DEBUG', |
| 140 | + self::Info => 'INFO', |
| 141 | + self::Notice => 'NOTICE', |
| 142 | + self::Warning => 'WARNING', |
| 143 | + self::Error => 'ERROR', |
| 144 | + self::Critical => 'CRITICAL', |
| 145 | + self::Alert => 'ALERT', |
| 146 | + self::Emergency => 'EMERGENCY', |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns the PSR-3 level matching this instance |
| 152 | + * |
| 153 | + * @phpstan-return \Psr\Log\LogLevel::* |
| 154 | + */ |
| 155 | + public function toPsrLogLevel(): string |
| 156 | + { |
| 157 | + return match ($this) { |
| 158 | + self::Debug => LogLevel::DEBUG, |
| 159 | + self::Info => LogLevel::INFO, |
| 160 | + self::Notice => LogLevel::NOTICE, |
| 161 | + self::Warning => LogLevel::WARNING, |
| 162 | + self::Error => LogLevel::ERROR, |
| 163 | + self::Critical => LogLevel::CRITICAL, |
| 164 | + self::Alert => LogLevel::ALERT, |
| 165 | + self::Emergency => LogLevel::EMERGENCY, |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns the RFC 5424 level matching this instance |
| 171 | + * |
| 172 | + * @phpstan-return int<0, 7> |
| 173 | + */ |
| 174 | + public function toRFC5424Level(): int |
| 175 | + { |
| 176 | + return match ($this) { |
| 177 | + self::Debug => 7, |
| 178 | + self::Info => 6, |
| 179 | + self::Notice => 5, |
| 180 | + self::Warning => 4, |
| 181 | + self::Error => 3, |
| 182 | + self::Critical => 2, |
| 183 | + self::Alert => 1, |
| 184 | + self::Emergency => 0, |
| 185 | + }; |
| 186 | + } |
| 187 | + |
| 188 | + public const VALUES = [ |
| 189 | + 100, |
| 190 | + 200, |
| 191 | + 250, |
| 192 | + 300, |
| 193 | + 400, |
| 194 | + 500, |
| 195 | + 550, |
| 196 | + 600, |
| 197 | + ]; |
| 198 | + |
| 199 | + public const NAMES = [ |
| 200 | + 'DEBUG', |
| 201 | + 'INFO', |
| 202 | + 'NOTICE', |
| 203 | + 'WARNING', |
| 204 | + 'ERROR', |
| 205 | + 'CRITICAL', |
| 206 | + 'ALERT', |
| 207 | + 'EMERGENCY', |
| 208 | + ]; |
| 209 | +} |
0 commit comments