Level.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @see LevelName
  14. */
  15. enum Level: int
  16. {
  17. /**
  18. * Detailed debug information
  19. */
  20. case Debug = 100;
  21. /**
  22. * Interesting events
  23. *
  24. * Examples: User logs in, SQL logs.
  25. */
  26. case Info = 200;
  27. /**
  28. * Uncommon events
  29. */
  30. case Notice = 250;
  31. /**
  32. * Exceptional occurrences that are not errors
  33. *
  34. * Examples: Use of deprecated APIs, poor use of an API,
  35. * undesirable things that are not necessarily wrong.
  36. */
  37. case Warning = 300;
  38. /**
  39. * Runtime errors
  40. */
  41. case Error = 400;
  42. /**
  43. * Critical conditions
  44. *
  45. * Example: Application component unavailable, unexpected exception.
  46. */
  47. case Critical = 500;
  48. /**
  49. * Action must be taken immediately
  50. *
  51. * Example: Entire website down, database unavailable, etc.
  52. * This should trigger the SMS alerts and wake you up.
  53. */
  54. case Alert = 550;
  55. /**
  56. * Urgent alert.
  57. */
  58. case Emergency = 600;
  59. public static function fromLevelName(LevelName $name): self
  60. {
  61. return match ($name) {
  62. LevelName::Debug => self::Debug,
  63. LevelName::Info => self::Info,
  64. LevelName::Notice => self::Notice,
  65. LevelName::Warning => self::Warning,
  66. LevelName::Error => self::Error,
  67. LevelName::Critical => self::Critical,
  68. LevelName::Alert => self::Alert,
  69. LevelName::Emergency => self::Emergency,
  70. };
  71. }
  72. /**
  73. * Returns true if the passed $level is higher or equal to $this
  74. */
  75. public function includes(Level $level): bool
  76. {
  77. return $this->value <= $level->value;
  78. }
  79. public function isHigherThan(Level $level): bool
  80. {
  81. return $this->value > $level->value;
  82. }
  83. public function isLowerThan(Level $level): bool
  84. {
  85. return $this->value < $level->value;
  86. }
  87. public function toLevelName(): LevelName
  88. {
  89. return LevelName::fromLevel($this);
  90. }
  91. /**
  92. * @phpstan-return \Psr\Log\LogLevel::*
  93. */
  94. public function toPsrLogLevel(): string
  95. {
  96. return match ($this) {
  97. self::Debug => LogLevel::DEBUG,
  98. self::Info => LogLevel::INFO,
  99. self::Notice => LogLevel::NOTICE,
  100. self::Warning => LogLevel::WARNING,
  101. self::Error => LogLevel::ERROR,
  102. self::Critical => LogLevel::CRITICAL,
  103. self::Alert => LogLevel::ALERT,
  104. self::Emergency => LogLevel::EMERGENCY,
  105. };
  106. }
  107. public const VALUES = [
  108. 100,
  109. 200,
  110. 250,
  111. 300,
  112. 400,
  113. 500,
  114. 550,
  115. 600,
  116. ];
  117. }