Logger.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace Nekonomokochan\PhpJsonLogger;
  3. use Ramsey\Uuid\Uuid;
  4. /**
  5. * Class Logger
  6. *
  7. * @package Nekonomokochan\PhpJsonLogger
  8. */
  9. class Logger extends \Monolog\Logger
  10. {
  11. use ErrorsContextFormatter;
  12. use MonologCreator;
  13. /**
  14. * @var string
  15. */
  16. private $traceId;
  17. /**
  18. * @var string
  19. * @see \Monolog\Logger::$name
  20. */
  21. private $channel;
  22. /**
  23. * @var int
  24. */
  25. private $logLevel;
  26. /**
  27. * @var string
  28. */
  29. private $logFileName;
  30. /**
  31. * @var int
  32. * @see \Monolog\Handler\RotatingFileHandler::$maxFiles
  33. */
  34. private $maxFiles;
  35. /**
  36. * Logger constructor.
  37. *
  38. * @param LoggerBuilder $builder
  39. * @throws \Exception
  40. */
  41. public function __construct(LoggerBuilder $builder)
  42. {
  43. $this->traceId = $builder->getTraceId();
  44. $this->generateTraceIdIfNeeded();
  45. $this->channel = $builder->getChannel();
  46. $this->logFileName = $builder->getFileName();
  47. $this->logLevel = $builder->getLogLevel();
  48. $this->maxFiles = $builder->getMaxFiles();
  49. $constructParams = $this->createConstructParams($this->traceId, $builder);
  50. parent::__construct(
  51. $constructParams['channel'],
  52. $constructParams['handlers'],
  53. $constructParams['processors']
  54. );
  55. }
  56. /**
  57. * @param $message
  58. * @param $context
  59. */
  60. public function debug($message, $context = '')
  61. {
  62. $context = $this->formatParamToArray($context);
  63. $this->addDebug($message, $context);
  64. }
  65. /**
  66. * @param $message
  67. * @param $context
  68. */
  69. public function info($message, $context = '')
  70. {
  71. $context = $this->formatParamToArray($context);
  72. $this->addInfo($message, $context);
  73. }
  74. /**
  75. * @param $message
  76. * @param $context
  77. */
  78. public function notice($message, $context = '')
  79. {
  80. $context = $this->formatParamToArray($context);
  81. $this->addNotice($message, $context);
  82. }
  83. /**
  84. * @param $message
  85. * @param $context
  86. */
  87. public function warning($message, $context = '')
  88. {
  89. $context = $this->formatParamToArray($context);
  90. $this->addWarning($message, $context);
  91. }
  92. /**
  93. * @param \Throwable $e
  94. * @param $context
  95. */
  96. public function error($e, $context = '')
  97. {
  98. if ($this->isErrorObject($e) === false) {
  99. throw new \InvalidArgumentException(
  100. $this->generateInvalidArgumentMessage(__METHOD__)
  101. );
  102. }
  103. $context = $this->formatParamToArray($context);
  104. $this->addError(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  105. }
  106. /**
  107. * @param \Throwable $e
  108. * @param $context
  109. */
  110. public function critical($e, $context = '')
  111. {
  112. if ($this->isErrorObject($e) === false) {
  113. throw new \InvalidArgumentException(
  114. $this->generateInvalidArgumentMessage(__METHOD__)
  115. );
  116. }
  117. $context = $this->formatParamToArray($context);
  118. $this->addCritical(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  119. }
  120. /**
  121. * @param \Throwable $e
  122. * @param $context
  123. */
  124. public function alert($e, $context = '')
  125. {
  126. if ($this->isErrorObject($e) === false) {
  127. throw new \InvalidArgumentException(
  128. $this->generateInvalidArgumentMessage(__METHOD__)
  129. );
  130. }
  131. $context = $this->formatParamToArray($context);
  132. $this->addAlert(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  133. }
  134. /**
  135. * @param \Throwable $e
  136. * @param $context
  137. */
  138. public function emergency($e, $context = '')
  139. {
  140. if ($this->isErrorObject($e) === false) {
  141. throw new \InvalidArgumentException(
  142. $this->generateInvalidArgumentMessage(__METHOD__)
  143. );
  144. }
  145. $context = $this->formatParamToArray($context);
  146. $this->addEmergency(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getTraceId(): string
  152. {
  153. return $this->traceId;
  154. }
  155. public function setUsername(string $username)
  156. {
  157. $this->traceId = $username;
  158. }
  159. /**
  160. * @return string
  161. */
  162. public function getChannel(): string
  163. {
  164. return $this->channel;
  165. }
  166. public function setChannel(string $channel)
  167. {
  168. $this->channel = $channel;
  169. }
  170. /**
  171. * @return int
  172. */
  173. public function getLogLevel(): int
  174. {
  175. return $this->logLevel;
  176. }
  177. /**
  178. * @return string
  179. */
  180. public function getLogFileName(): string
  181. {
  182. return $this->logFileName;
  183. }
  184. /**
  185. * @return int
  186. */
  187. public function getMaxFiles(): int
  188. {
  189. return $this->maxFiles;
  190. }
  191. /**
  192. * Generate if TraceID is empty
  193. */
  194. private function generateTraceIdIfNeeded()
  195. {
  196. if (empty($this->traceId)) {
  197. $this->traceId = Uuid::uuid4()->toString();
  198. }
  199. }
  200. /**
  201. * @param $value
  202. * @return bool
  203. */
  204. private function isErrorObject($value): bool
  205. {
  206. if ($value instanceof \Exception || $value instanceof \Error) {
  207. return true;
  208. }
  209. return false;
  210. }
  211. /**
  212. * @param string $method
  213. * @return string
  214. */
  215. private function generateInvalidArgumentMessage(string $method)
  216. {
  217. return 'Please give the exception class to the ' . $method;
  218. }
  219. private function formatParamToArray($value): array
  220. {
  221. if (is_array($value)) {
  222. return $value;
  223. } else {
  224. return (empty($value)) ? [] : ['data' => $value];
  225. }
  226. }
  227. }