PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php

https://gitlab.com/judielsm/Handora
PHP | 146 lines | 91 code | 17 blank | 38 comment | 15 complexity | 841979a4004cbb61b984d1df390b54d8 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. /**
  13. * Stores to any stream resource
  14. *
  15. * Can be used to store into php://stderr, remote and local files, etc.
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class StreamHandler extends AbstractProcessingHandler
  20. {
  21. protected $stream;
  22. protected $url;
  23. private $errorMessage;
  24. protected $filePermission;
  25. protected $useLocking;
  26. private $dirCreated;
  27. /**
  28. * @param resource|string $stream
  29. * @param integer $level The minimum logging level at which this handler will be triggered
  30. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  31. * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
  32. * @param Boolean $useLocking Try to lock log file before doing any writes
  33. *
  34. * @throws \Exception If a missing directory is not buildable
  35. * @throws \InvalidArgumentException If stream is not a resource or string
  36. */
  37. public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
  38. {
  39. parent::__construct($level, $bubble);
  40. if (is_resource($stream)) {
  41. $this->stream = $stream;
  42. } elseif (is_string($stream)) {
  43. $this->url = $stream;
  44. } else {
  45. throw new \InvalidArgumentException('A stream must either be a resource or a string.');
  46. }
  47. $this->filePermission = $filePermission;
  48. $this->useLocking = $useLocking;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function close()
  54. {
  55. if (is_resource($this->stream)) {
  56. fclose($this->stream);
  57. }
  58. $this->stream = null;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function write(array $record)
  64. {
  65. if (!is_resource($this->stream)) {
  66. if (!$this->url) {
  67. throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
  68. }
  69. $this->createDir();
  70. $this->errorMessage = null;
  71. set_error_handler(array($this, 'customErrorHandler'));
  72. $this->stream = fopen($this->url, 'a');
  73. if ($this->filePermission !== null) {
  74. @chmod($this->url, $this->filePermission);
  75. }
  76. restore_error_handler();
  77. if (!is_resource($this->stream)) {
  78. $this->stream = null;
  79. throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
  80. }
  81. }
  82. if ($this->useLocking) {
  83. // ignoring errors here, there's not much we can do about them
  84. flock($this->stream, LOCK_EX);
  85. }
  86. fwrite($this->stream, (string) $record['formatted']);
  87. if ($this->useLocking) {
  88. flock($this->stream, LOCK_UN);
  89. }
  90. }
  91. private function customErrorHandler($code, $msg)
  92. {
  93. $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
  94. }
  95. /**
  96. * @param string $stream
  97. *
  98. * @return null|string
  99. */
  100. private function getDirFromStream($stream)
  101. {
  102. $pos = strpos($stream, '://');
  103. if ($pos === false) {
  104. return dirname($stream);
  105. }
  106. if ('file://' === substr($stream, 0, 7)) {
  107. return dirname(substr($stream, 7));
  108. }
  109. return;
  110. }
  111. private function createDir()
  112. {
  113. // Do not try to create dir if it has already been tried.
  114. if ($this->dirCreated) {
  115. return;
  116. }
  117. $dir = $this->getDirFromStream($this->url);
  118. if (null !== $dir && !is_dir($dir)) {
  119. $this->errorMessage = null;
  120. set_error_handler(array($this, 'customErrorHandler'));
  121. $status = mkdir($dir, 0777, true);
  122. restore_error_handler();
  123. if (false === $status) {
  124. throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and its not buildable: '.$this->errorMessage, $dir));
  125. }
  126. }
  127. $this->dirCreated = true;
  128. }
  129. }