Level.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 out of a Level there are three 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 ->name to get the enum case's name which is capitalized (e.g. "Debug")
  23. *
  24. * To get the value for filtering, if the includes/isLowerThan/isHigherThan methods are
  25. * not enough, you can use ->value to get the enum case's integer value.
  26. */
  27. enum Level: int
  28. {
  29. /**
  30. * Detailed debug information
  31. */
  32. case Debug = 100;
  33. /**
  34. * Interesting events
  35. *
  36. * Examples: User logs in, SQL logs.
  37. */
  38. case Info = 200;
  39. /**
  40. * Uncommon events
  41. */
  42. case Notice = 250;
  43. /**
  44. * Exceptional occurrences that are not errors
  45. *
  46. * Examples: Use of deprecated APIs, poor use of an API,
  47. * undesirable things that are not necessarily wrong.
  48. */
  49. case Warning = 300;
  50. /**
  51. * Runtime errors
  52. */
  53. case Error = 400;
  54. /**
  55. * Critical conditions
  56. *
  57. * Example: Application component unavailable, unexpected exception.
  58. */
  59. case Critical = 500;
  60. /**
  61. * Action must be taken immediately
  62. *
  63. * Example: Entire website down, database unavailable, etc.
  64. * This should trigger the SMS alerts and wake you up.
  65. */
  66. case Alert = 550;
  67. /**
  68. * Urgent alert.
  69. */
  70. case Emergency = 600;
  71. /**
  72. * @param value-of<self::NAMES>|LogLevel::*|'Debug'|'Info'|'Notice'|'Warning'|'Error'|'Critical'|'Alert'|'Emergency' $name
  73. * @return static
  74. */
  75. public static function fromName(string $name): self
  76. {
  77. return match ($name) {
  78. 'debug', 'Debug', 'DEBUG' => self::Debug,
  79. 'info', 'Info', 'INFO' => self::Info,
  80. 'notice', 'Notice', 'NOTICE' => self::Notice,
  81. 'warning', 'Warning', 'WARNING' => self::Warning,
  82. 'error', 'Error', 'ERROR' => self::Error,
  83. 'critical', 'Critical', 'CRITICAL' => self::Critical,
  84. 'alert', 'Alert', 'ALERT' => self::Alert,
  85. 'emergency', 'Emergency', 'EMERGENCY' => self::Emergency,
  86. };
  87. }
  88. /**
  89. * @param value-of<self::VALUES> $value
  90. * @return static
  91. */
  92. public static function fromValue(int $value): self
  93. {
  94. return self::from($value);
  95. }
  96. /**
  97. * Returns true if the passed $level is higher or equal to $this
  98. */
  99. public function includes(Level $level): bool
  100. {
  101. return $this->value <= $level->value;
  102. }
  103. public function isHigherThan(Level $level): bool
  104. {
  105. return $this->value > $level->value;
  106. }
  107. public function isLowerThan(Level $level): bool
  108. {
  109. return $this->value < $level->value;
  110. }
  111. /**
  112. * Returns the monolog standardized all-capitals name of the level
  113. *
  114. * Use this instead of $level->name which returns the enum case name (e.g. Debug vs DEBUG if you use getName())
  115. *
  116. * @return value-of<self::NAMES>
  117. */
  118. public function getName(): string
  119. {
  120. return match ($this) {
  121. self::Debug => 'DEBUG',
  122. self::Info => 'INFO',
  123. self::Notice => 'NOTICE',
  124. self::Warning => 'WARNING',
  125. self::Error => 'ERROR',
  126. self::Critical => 'CRITICAL',
  127. self::Alert => 'ALERT',
  128. self::Emergency => 'EMERGENCY',
  129. };
  130. }
  131. /**
  132. * @phpstan-return \Psr\Log\LogLevel::*
  133. */
  134. public function toPsrLogLevel(): string
  135. {
  136. return match ($this) {
  137. self::Debug => LogLevel::DEBUG,
  138. self::Info => LogLevel::INFO,
  139. self::Notice => LogLevel::NOTICE,
  140. self::Warning => LogLevel::WARNING,
  141. self::Error => LogLevel::ERROR,
  142. self::Critical => LogLevel::CRITICAL,
  143. self::Alert => LogLevel::ALERT,
  144. self::Emergency => LogLevel::EMERGENCY,
  145. };
  146. }
  147. public const VALUES = [
  148. 100,
  149. 200,
  150. 250,
  151. 300,
  152. 400,
  153. 500,
  154. 550,
  155. 600,
  156. ];
  157. public const NAMES = [
  158. 'DEBUG',
  159. 'INFO',
  160. 'NOTICE',
  161. 'WARNING',
  162. 'ERROR',
  163. 'CRITICAL',
  164. 'ALERT',
  165. 'EMERGENCY',
  166. ];
  167. }