LoggerTrait.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * @param string $message
  17. * @param array $context
  18. *
  19. * @return void
  20. */
  21. public function emergency($message, array $context = array())
  22. {
  23. $this->log(LogLevel::EMERGENCY, $message, $context);
  24. }
  25. /**
  26. * Action must be taken immediately.
  27. *
  28. * Example: Entire website down, database unavailable, etc. This should
  29. * trigger the SMS alerts and wake you up.
  30. *
  31. * @param string $message
  32. * @param array $context
  33. *
  34. * @return void
  35. */
  36. public function alert($message, array $context = array())
  37. {
  38. $this->log(LogLevel::ALERT, $message, $context);
  39. }
  40. /**
  41. * Critical conditions.
  42. *
  43. * Example: Application component unavailable, unexpected exception.
  44. *
  45. * @param string $message
  46. * @param array $context
  47. *
  48. * @return void
  49. */
  50. public function critical($message, array $context = array())
  51. {
  52. $this->log(LogLevel::CRITICAL, $message, $context);
  53. }
  54. /**
  55. * Runtime errors that do not require immediate action but should typically
  56. * be logged and monitored.
  57. *
  58. * @param string $message
  59. * @param array $context
  60. *
  61. * @return void
  62. */
  63. public function error($message, array $context = array())
  64. {
  65. $this->log(LogLevel::ERROR, $message, $context);
  66. }
  67. /**
  68. * Exceptional occurrences that are not errors.
  69. *
  70. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  71. * that are not necessarily wrong.
  72. *
  73. * @param string $message
  74. * @param array $context
  75. *
  76. * @return void
  77. */
  78. public function warning($message, array $context = array())
  79. {
  80. $this->log(LogLevel::WARNING, $message, $context);
  81. }
  82. /**
  83. * Normal but significant events.
  84. *
  85. * @param string $message
  86. * @param array $context
  87. *
  88. * @return void
  89. */
  90. public function notice($message, array $context = array())
  91. {
  92. $this->log(LogLevel::NOTICE, $message, $context);
  93. }
  94. /**
  95. * Interesting events.
  96. *
  97. * Example: User logs in, SQL logs.
  98. *
  99. * @param string $message
  100. * @param array $context
  101. *
  102. * @return void
  103. */
  104. public function info($message, array $context = array())
  105. {
  106. $this->log(LogLevel::INFO, $message, $context);
  107. }
  108. /**
  109. * Detailed debug information.
  110. *
  111. * @param string $message
  112. * @param array $context
  113. *
  114. * @return void
  115. */
  116. public function debug($message, array $context = array())
  117. {
  118. $this->log(LogLevel::DEBUG, $message, $context);
  119. }
  120. /**
  121. * Logs with an arbitrary level.
  122. *
  123. * @param mixed $level
  124. * @param string $message
  125. * @param array $context
  126. *
  127. * @return void
  128. */
  129. abstract public function log($level, $message, array $context = array());
  130. }