LoggerTrait.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Psr\Log;
  3. /**
  4. * This is a simple Logger trait that classes unable to extend AbstractLogger
  5. * (because they extend another class, etc) can include.
  6. *
  7. * It simply delegates all log-level-specific methods to the `log` method to
  8. * reduce boilerplate code that a simple Logger that does the same thing with
  9. * messages regardless of the error level has to implement.
  10. */
  11. trait LoggerTrait
  12. {
  13. /**
  14. * System is unusable.
  15. */
  16. public function emergency(string|\Stringable $message, array $context = []): void
  17. {
  18. $this->log(LogLevel::EMERGENCY, $message, $context);
  19. }
  20. /**
  21. * Action must be taken immediately.
  22. *
  23. * Example: Entire website down, database unavailable, etc. This should
  24. * trigger the SMS alerts and wake you up.
  25. */
  26. public function alert(string|\Stringable $message, array $context = []): void
  27. {
  28. $this->log(LogLevel::ALERT, $message, $context);
  29. }
  30. /**
  31. * Critical conditions.
  32. *
  33. * Example: Application component unavailable, unexpected exception.
  34. */
  35. public function critical(string|\Stringable $message, array $context = []): void
  36. {
  37. $this->log(LogLevel::CRITICAL, $message, $context);
  38. }
  39. /**
  40. * Runtime errors that do not require immediate action but should typically
  41. * be logged and monitored.
  42. */
  43. public function error(string|\Stringable $message, array $context = []): void
  44. {
  45. $this->log(LogLevel::ERROR, $message, $context);
  46. }
  47. /**
  48. * Exceptional occurrences that are not errors.
  49. *
  50. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  51. * that are not necessarily wrong.
  52. */
  53. public function warning(string|\Stringable $message, array $context = []): void
  54. {
  55. $this->log(LogLevel::WARNING, $message, $context);
  56. }
  57. /**
  58. * Normal but significant events.
  59. */
  60. public function notice(string|\Stringable $message, array $context = []): void
  61. {
  62. $this->log(LogLevel::NOTICE, $message, $context);
  63. }
  64. /**
  65. * Interesting events.
  66. *
  67. * Example: User logs in, SQL logs.
  68. */
  69. public function info(string|\Stringable $message, array $context = []): void
  70. {
  71. $this->log(LogLevel::INFO, $message, $context);
  72. }
  73. /**
  74. * Detailed debug information.
  75. */
  76. public function debug(string|\Stringable $message, array $context = []): void
  77. {
  78. $this->log(LogLevel::DEBUG, $message, $context);
  79. }
  80. /**
  81. * Logs with an arbitrary level.
  82. *
  83. * @param mixed $level
  84. *
  85. * @throws \Psr\Log\InvalidArgumentException
  86. */
  87. abstract public function log($level, string|\Stringable $message, array $context = []): void;
  88. }