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

https://gitlab.com/techniconline/kmc · PHP · 375 lines · 152 code · 38 blank · 185 comment · 5 complexity · eada70ccbf5e6d35a12169ee5fbc5357 MD5 · raw file

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