loggerExt.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. use Nekonomokochan\PhpJsonLogger\Logger;
  3. class OrganizrLoggerExt extends Logger
  4. {
  5. /**
  6. * @param $message
  7. * @param $context
  8. */
  9. public function debug($message, $context = '')
  10. {
  11. $context = $this->formatParamToArray($context);
  12. $this->addDebug($message, $context);
  13. }
  14. /**
  15. * @param $message
  16. * @param $context
  17. */
  18. public function info($message, $context = '')
  19. {
  20. $context = $this->formatParamToArray($context);
  21. $this->addInfo($message, $context);
  22. }
  23. /**
  24. * @param $message
  25. * @param $context
  26. */
  27. public function notice($message, $context = '')
  28. {
  29. $context = $this->formatParamToArray($context);
  30. $this->addNotice($message, $context);
  31. }
  32. /**
  33. * @param $message
  34. * @param $context
  35. */
  36. public function warning($message, $context = '')
  37. {
  38. $context = $this->formatParamToArray($context);
  39. $this->addWarning($message, $context);
  40. }
  41. /**
  42. * @param \Throwable $e
  43. * @param $context
  44. */
  45. public function error($e, $context = '')
  46. {
  47. $context = $this->formatParamToArray($context);
  48. if ($this->isErrorObject($e) === false) {
  49. throw new \InvalidArgumentException(
  50. $this->generateInvalidArgumentMessage(__METHOD__)
  51. );
  52. }
  53. $this->addError(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  54. }
  55. /**
  56. * @param \Throwable $e
  57. * @param $context
  58. */
  59. public function critical($e, $context = '')
  60. {
  61. $context = $this->formatParamToArray($context);
  62. if ($this->isErrorObject($e) === false) {
  63. throw new \InvalidArgumentException(
  64. $this->generateInvalidArgumentMessage(__METHOD__)
  65. );
  66. }
  67. $this->addCritical(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  68. }
  69. /**
  70. * @param \Throwable $e
  71. * @param $context
  72. */
  73. public function alert($e, $context = '')
  74. {
  75. $context = $this->formatParamToArray($context);
  76. if ($this->isErrorObject($e) === false) {
  77. throw new \InvalidArgumentException(
  78. $this->generateInvalidArgumentMessage(__METHOD__)
  79. );
  80. }
  81. $this->addAlert(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  82. }
  83. /**
  84. * @param \Throwable $e
  85. * @param $context
  86. */
  87. public function emergency($e, $context = '')
  88. {
  89. $context = $this->formatParamToArray($context);
  90. if ($this->isErrorObject($e) === false) {
  91. throw new \InvalidArgumentException(
  92. $this->generateInvalidArgumentMessage(__METHOD__)
  93. );
  94. }
  95. $this->addEmergency(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  96. }
  97. /**
  98. * @param $value
  99. * @return bool
  100. */
  101. private function isErrorObject($value): bool
  102. {
  103. if ($value instanceof \Exception || $value instanceof \Error) {
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * @param $value
  110. * @return array
  111. */
  112. private function formatParamToArray($value): array
  113. {
  114. if (is_array($value)) {
  115. return $value;
  116. } else {
  117. return (empty($value)) ? [] : ['context' => $value];
  118. }
  119. }
  120. /**
  121. * @param string $method
  122. * @return string
  123. */
  124. private function generateInvalidArgumentMessage(string $method): string
  125. {
  126. return 'Please give the exception class to the ' . $method;
  127. }
  128. }