Level.php 5.4 KB

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