LoggerTrait.php 3.2 KB

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