PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Log/Writer.php

https://gitlab.com/judielsm/Handora
PHP | 375 lines | 153 code | 37 blank | 185 comment | 5 complexity | 1fe1a4d3e08936ff819e0f85093965fb MD5 | raw file
  1. <?php
  2. namespace Illuminate\Log;
  3. use Closure;
  4. use RuntimeException;
  5. use InvalidArgumentException;
  6. use Monolog\Handler\SyslogHandler;
  7. use Monolog\Handler\StreamHandler;
  8. use Monolog\Logger as MonologLogger;
  9. use Monolog\Formatter\LineFormatter;
  10. use Monolog\Handler\ErrorLogHandler;
  11. use Monolog\Handler\RotatingFileHandler;
  12. use Illuminate\Contracts\Support\Jsonable;
  13. use Illuminate\Contracts\Events\Dispatcher;
  14. use Illuminate\Contracts\Support\Arrayable;
  15. use Psr\Log\LoggerInterface as PsrLoggerInterface;
  16. use Illuminate\Contracts\Logging\Log as LogContract;
  17. class Writer implements LogContract, PsrLoggerInterface
  18. {
  19. /**
  20. * The Monolog logger instance.
  21. *
  22. * @var \Monolog\Logger
  23. */
  24. protected $monolog;
  25. /**
  26. * The event dispatcher instance.
  27. *
  28. * @var \Illuminate\Contracts\Events\Dispatcher
  29. */
  30. protected $dispatcher;
  31. /**
  32. * The Log levels.
  33. *
  34. * @var array
  35. */
  36. protected $levels = [
  37. 'debug' => MonologLogger::DEBUG,
  38. 'info' => MonologLogger::INFO,
  39. 'notice' => MonologLogger::NOTICE,
  40. 'warning' => MonologLogger::WARNING,
  41. 'error' => MonologLogger::ERROR,
  42. 'critical' => MonologLogger::CRITICAL,
  43. 'alert' => MonologLogger::ALERT,
  44. 'emergency' => MonologLogger::EMERGENCY,
  45. ];
  46. /**
  47. * Create a new log writer instance.
  48. *
  49. * @param \Monolog\Logger $monolog
  50. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  51. * @return void
  52. */
  53. public function __construct(MonologLogger $monolog, Dispatcher $dispatcher = null)
  54. {
  55. $this->monolog = $monolog;
  56. if (isset($dispatcher)) {
  57. $this->dispatcher = $dispatcher;
  58. }
  59. }
  60. /**
  61. * Log an emergency message to the logs.
  62. *
  63. * @param string $message
  64. * @param array $context
  65. * @return void
  66. */
  67. public function emergency($message, array $context = [])
  68. {
  69. return $this->writeLog(__FUNCTION__, $message, $context);
  70. }
  71. /**
  72. * Log an alert message to the logs.
  73. *
  74. * @param string $message
  75. * @param array $context
  76. * @return void
  77. */
  78. public function alert($message, array $context = [])
  79. {
  80. return $this->writeLog(__FUNCTION__, $message, $context);
  81. }
  82. /**
  83. * Log a critical message to the logs.
  84. *
  85. * @param string $message
  86. * @param array $context
  87. * @return void
  88. */
  89. public function critical($message, array $context = [])
  90. {
  91. return $this->writeLog(__FUNCTION__, $message, $context);
  92. }
  93. /**
  94. * Log an error message to the logs.
  95. *
  96. * @param string $message
  97. * @param array $context
  98. * @return void
  99. */
  100. public function error($message, array $context = [])
  101. {
  102. return $this->writeLog(__FUNCTION__, $message, $context);
  103. }
  104. /**
  105. * Log a warning message to the logs.
  106. *
  107. * @param string $message
  108. * @param array $context
  109. * @return void
  110. */
  111. public function warning($message, array $context = [])
  112. {
  113. return $this->writeLog(__FUNCTION__, $message, $context);
  114. }
  115. /**
  116. * Log a notice to the logs.
  117. *
  118. * @param string $message
  119. * @param array $context
  120. * @return void
  121. */
  122. public function notice($message, array $context = [])
  123. {
  124. return $this->writeLog(__FUNCTION__, $message, $context);
  125. }
  126. /**
  127. * Log an informational message to the logs.
  128. *
  129. * @param string $message
  130. * @param array $context
  131. * @return void
  132. */
  133. public function info($message, array $context = [])
  134. {
  135. return $this->writeLog(__FUNCTION__, $message, $context);
  136. }
  137. /**
  138. * Log a debug message to the logs.
  139. *
  140. * @param string $message
  141. * @param array $context
  142. * @return void
  143. */
  144. public function debug($message, array $context = [])
  145. {
  146. return $this->writeLog(__FUNCTION__, $message, $context);
  147. }
  148. /**
  149. * Log a message to the logs.
  150. *
  151. * @param string $level
  152. * @param string $message
  153. * @param array $context
  154. * @return void
  155. */
  156. public function log($level, $message, array $context = [])
  157. {
  158. return $this->writeLog($level, $message, $context);
  159. }
  160. /**
  161. * Dynamically pass log calls into the writer.
  162. *
  163. * @param string $level
  164. * @param string $message
  165. * @param array $context
  166. * @return void
  167. */
  168. public function write($level, $message, array $context = [])
  169. {
  170. return $this->writeLog($level, $message, $context);
  171. }
  172. /**
  173. * Write a message to Monolog.
  174. *
  175. * @param string $level
  176. * @param string $message
  177. * @param array $context
  178. * @return void
  179. */
  180. protected function writeLog($level, $message, $context)
  181. {
  182. $this->fireLogEvent($level, $message = $this->formatMessage($message), $context);
  183. $this->monolog->{$level}($message, $context);
  184. }
  185. /**
  186. * Register a file log handler.
  187. *
  188. * @param string $path
  189. * @param string $level
  190. * @return void
  191. */
  192. public function useFiles($path, $level = 'debug')
  193. {
  194. $this->monolog->pushHandler($handler = new StreamHandler($path, $this->parseLevel($level)));
  195. $handler->setFormatter($this->getDefaultFormatter());
  196. }
  197. /**
  198. * Register a daily file log handler.
  199. *
  200. * @param string $path
  201. * @param int $days
  202. * @param string $level
  203. * @return void
  204. */
  205. public function useDailyFiles($path, $days = 0, $level = 'debug')
  206. {
  207. $this->monolog->pushHandler(
  208. $handler = new RotatingFileHandler($path, $days, $this->parseLevel($level))
  209. );
  210. $handler->setFormatter($this->getDefaultFormatter());
  211. }
  212. /**
  213. * Register a Syslog handler.
  214. *
  215. * @param string $name
  216. * @param string $level
  217. * @return void
  218. */
  219. public function useSyslog($name = 'laravel', $level = 'debug')
  220. {
  221. return $this->monolog->pushHandler(new SyslogHandler($name, LOG_USER, $level));
  222. }
  223. /**
  224. * Register an error_log handler.
  225. *
  226. * @param string $level
  227. * @param int $messageType
  228. * @return void
  229. */
  230. public function useErrorLog($level = 'debug', $messageType = ErrorLogHandler::OPERATING_SYSTEM)
  231. {
  232. $this->monolog->pushHandler(
  233. $handler = new ErrorLogHandler($messageType, $this->parseLevel($level))
  234. );
  235. $handler->setFormatter($this->getDefaultFormatter());
  236. }
  237. /**
  238. * Register a new callback handler for when a log event is triggered.
  239. *
  240. * @param \Closure $callback
  241. * @return void
  242. *
  243. * @throws \RuntimeException
  244. */
  245. public function listen(Closure $callback)
  246. {
  247. if (! isset($this->dispatcher)) {
  248. throw new RuntimeException('Events dispatcher has not been set.');
  249. }
  250. $this->dispatcher->listen('illuminate.log', $callback);
  251. }
  252. /**
  253. * Fires a log event.
  254. *
  255. * @param string $level
  256. * @param string $message
  257. * @param array $context
  258. * @return void
  259. */
  260. protected function fireLogEvent($level, $message, array $context = [])
  261. {
  262. // If the event dispatcher is set, we will pass along the parameters to the
  263. // log listeners. These are useful for building profilers or other tools
  264. // that aggregate all of the log messages for a given "request" cycle.
  265. if (isset($this->dispatcher)) {
  266. $this->dispatcher->fire('illuminate.log', compact('level', 'message', 'context'));
  267. }
  268. }
  269. /**
  270. * Format the parameters for the logger.
  271. *
  272. * @param mixed $message
  273. * @return void
  274. */
  275. protected function formatMessage($message)
  276. {
  277. if (is_array($message)) {
  278. return var_export($message, true);
  279. } elseif ($message instanceof Jsonable) {
  280. return $message->toJson();
  281. } elseif ($message instanceof Arrayable) {
  282. return var_export($message->toArray(), true);
  283. }
  284. return $message;
  285. }
  286. /**
  287. * Parse the string level into a Monolog constant.
  288. *
  289. * @param string $level
  290. * @return int
  291. *
  292. * @throws \InvalidArgumentException
  293. */
  294. protected function parseLevel($level)
  295. {
  296. if (isset($this->levels[$level])) {
  297. return $this->levels[$level];
  298. }
  299. throw new InvalidArgumentException('Invalid log level.');
  300. }
  301. /**
  302. * Get the underlying Monolog instance.
  303. *
  304. * @return \Monolog\Logger
  305. */
  306. public function getMonolog()
  307. {
  308. return $this->monolog;
  309. }
  310. /**
  311. * Get a defaut Monolog formatter instance.
  312. *
  313. * @return \Monolog\Formatter\LineFormatter
  314. */
  315. protected function getDefaultFormatter()
  316. {
  317. return new LineFormatter(null, null, true, true);
  318. }
  319. /**
  320. * Get the event dispatcher instance.
  321. *
  322. * @return \Illuminate\Contracts\Events\Dispatcher
  323. */
  324. public function getEventDispatcher()
  325. {
  326. return $this->dispatcher;
  327. }
  328. /**
  329. * Set the event dispatcher instance.
  330. *
  331. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  332. * @return void
  333. */
  334. public function setEventDispatcher(Dispatcher $dispatcher)
  335. {
  336. $this->dispatcher = $dispatcher;
  337. }
  338. }