StreamHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  27. * @param resource|string $stream
  28. * @param integer $level The minimum logging level at which this handler will be triggered
  29. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  30. * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
  31. * @param Boolean $useLocking Try to lock log file before doing any writes
  32. *
  33. * @throws InvalidArgumentException If stream is not a resource or string
  34. */
  35. public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
  36. {
  37. parent::__construct($level, $bubble);
  38. if (is_resource($stream)) {
  39. $this->stream = $stream;
  40. } elseif (is_string($stream)) {
  41. $this->url = $stream;
  42. } else {
  43. throw new \InvalidArgumentException('A stream must either be a resource or a string.');
  44. }
  45. $this->filePermission = $filePermission;
  46. $this->useLocking = $useLocking;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function close()
  52. {
  53. if (is_resource($this->stream)) {
  54. fclose($this->stream);
  55. }
  56. $this->stream = null;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function write(array $record)
  62. {
  63. if (!is_resource($this->stream)) {
  64. if (!$this->url) {
  65. throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
  66. }
  67. $this->errorMessage = null;
  68. set_error_handler(array($this, 'customErrorHandler'));
  69. $this->stream = fopen($this->url, 'a');
  70. if ($this->filePermission !== null) {
  71. @chmod($this->url, $this->filePermission);
  72. }
  73. restore_error_handler();
  74. if (!is_resource($this->stream)) {
  75. $this->stream = null;
  76. throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
  77. }
  78. }
  79. if ($this->useLocking) {
  80. // ignoring errors here, there's not much we can do about them
  81. flock($this->stream, LOCK_EX);
  82. }
  83. fwrite($this->stream, (string) $record['formatted']);
  84. if ($this->useLocking) {
  85. flock($this->stream, LOCK_UN);
  86. }
  87. }
  88. private function customErrorHandler($code, $msg)
  89. {
  90. $this->errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg);
  91. }
  92. }