Logger.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace Illuminate\Log;
  3. use Closure;
  4. use RuntimeException;
  5. use Psr\Log\LoggerInterface;
  6. use Illuminate\Log\Events\MessageLogged;
  7. use Illuminate\Contracts\Support\Jsonable;
  8. use Illuminate\Contracts\Events\Dispatcher;
  9. use Illuminate\Contracts\Support\Arrayable;
  10. class Logger implements LoggerInterface
  11. {
  12. /**
  13. * The underlying logger implementation.
  14. *
  15. * @var \Psr\Log\LoggerInterface
  16. */
  17. protected $logger;
  18. /**
  19. * The event dispatcher instance.
  20. *
  21. * @var \Illuminate\Contracts\Events\Dispatcher|null
  22. */
  23. protected $dispatcher;
  24. /**
  25. * Create a new log writer instance.
  26. *
  27. * @param \Psr\Log\LoggerInterface $logger
  28. * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher
  29. * @return void
  30. */
  31. public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null)
  32. {
  33. $this->logger = $logger;
  34. $this->dispatcher = $dispatcher;
  35. }
  36. /**
  37. * Log an emergency message to the logs.
  38. *
  39. * @param string $message
  40. * @param array $context
  41. * @return void
  42. */
  43. public function emergency($message, array $context = [])
  44. {
  45. $this->writeLog(__FUNCTION__, $message, $context);
  46. }
  47. /**
  48. * Log an alert message to the logs.
  49. *
  50. * @param string $message
  51. * @param array $context
  52. * @return void
  53. */
  54. public function alert($message, array $context = [])
  55. {
  56. $this->writeLog(__FUNCTION__, $message, $context);
  57. }
  58. /**
  59. * Log a critical message to the logs.
  60. *
  61. * @param string $message
  62. * @param array $context
  63. * @return void
  64. */
  65. public function critical($message, array $context = [])
  66. {
  67. $this->writeLog(__FUNCTION__, $message, $context);
  68. }
  69. /**
  70. * Log an error message to the logs.
  71. *
  72. * @param string $message
  73. * @param array $context
  74. * @return void
  75. */
  76. public function error($message, array $context = [])
  77. {
  78. $this->writeLog(__FUNCTION__, $message, $context);
  79. }
  80. /**
  81. * Log a warning message to the logs.
  82. *
  83. * @param string $message
  84. * @param array $context
  85. * @return void
  86. */
  87. public function warning($message, array $context = [])
  88. {
  89. $this->writeLog(__FUNCTION__, $message, $context);
  90. }
  91. /**
  92. * Log a notice to the logs.
  93. *
  94. * @param string $message
  95. * @param array $context
  96. * @return void
  97. */
  98. public function notice($message, array $context = [])
  99. {
  100. $this->writeLog(__FUNCTION__, $message, $context);
  101. }
  102. /**
  103. * Log an informational message to the logs.
  104. *
  105. * @param string $message
  106. * @param array $context
  107. * @return void
  108. */
  109. public function info($message, array $context = [])
  110. {
  111. $this->writeLog(__FUNCTION__, $message, $context);
  112. }
  113. /**
  114. * Log a debug message to the logs.
  115. *
  116. * @param string $message
  117. * @param array $context
  118. * @return void
  119. */
  120. public function debug($message, array $context = [])
  121. {
  122. $this->writeLog(__FUNCTION__, $message, $context);
  123. }
  124. /**
  125. * Log a message to the logs.
  126. *
  127. * @param string $level
  128. * @param string $message
  129. * @param array $context
  130. * @return void
  131. */
  132. public function log($level, $message, array $context = [])
  133. {
  134. $this->writeLog($level, $message, $context);
  135. }
  136. /**
  137. * Dynamically pass log calls into the writer.
  138. *
  139. * @param string $level
  140. * @param string $message
  141. * @param array $context
  142. * @return void
  143. */
  144. public function write($level, $message, array $context = [])
  145. {
  146. $this->writeLog($level, $message, $context);
  147. }
  148. /**
  149. * Write a message to the log.
  150. *
  151. * @param string $level
  152. * @param string $message
  153. * @param array $context
  154. * @return void
  155. */
  156. protected function writeLog($level, $message, $context)
  157. {
  158. $this->fireLogEvent($level, $message = $this->formatMessage($message), $context);
  159. $this->logger->{$level}($message, $context);
  160. }
  161. /**
  162. * Register a new callback handler for when a log event is triggered.
  163. *
  164. * @param \Closure $callback
  165. * @return void
  166. *
  167. * @throws \RuntimeException
  168. */
  169. public function listen(Closure $callback)
  170. {
  171. if (! isset($this->dispatcher)) {
  172. throw new RuntimeException('Events dispatcher has not been set.');
  173. }
  174. $this->dispatcher->listen(MessageLogged::class, $callback);
  175. }
  176. /**
  177. * Fires a log event.
  178. *
  179. * @param string $level
  180. * @param string $message
  181. * @param array $context
  182. * @return void
  183. */
  184. protected function fireLogEvent($level, $message, array $context = [])
  185. {
  186. // If the event dispatcher is set, we will pass along the parameters to the
  187. // log listeners. These are useful for building profilers or other tools
  188. // that aggregate all of the log messages for a given "request" cycle.
  189. if (isset($this->dispatcher)) {
  190. $this->dispatcher->dispatch(new MessageLogged($level, $message, $context));
  191. }
  192. }
  193. /**
  194. * Format the parameters for the logger.
  195. *
  196. * @param mixed $message
  197. * @return mixed
  198. */
  199. protected function formatMessage($message)
  200. {
  201. if (is_array($message)) {
  202. return var_export($message, true);
  203. } elseif ($message instanceof Jsonable) {
  204. return $message->toJson();
  205. } elseif ($message instanceof Arrayable) {
  206. return var_export($message->toArray(), true);
  207. }
  208. return $message;
  209. }
  210. /**
  211. * Get the underlying logger implementation.
  212. *
  213. * @return \Psr\Log\LoggerInterface
  214. */
  215. public function getLogger()
  216. {
  217. return $this->logger;
  218. }
  219. /**
  220. * Get the event dispatcher instance.
  221. *
  222. * @return \Illuminate\Contracts\Events\Dispatcher
  223. */
  224. public function getEventDispatcher()
  225. {
  226. return $this->dispatcher;
  227. }
  228. /**
  229. * Set the event dispatcher instance.
  230. *
  231. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  232. * @return void
  233. */
  234. public function setEventDispatcher(Dispatcher $dispatcher)
  235. {
  236. $this->dispatcher = $dispatcher;
  237. }
  238. /**
  239. * Dynamically proxy method calls to the underlying logger.
  240. *
  241. * @param string $method
  242. * @param array $parameters
  243. * @return mixed
  244. */
  245. public function __call($method, $parameters)
  246. {
  247. return $this->logger->{$method}(...$parameters);
  248. }
  249. }