StreamHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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. public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
  34. {
  35. parent::__construct($level, $bubble);
  36. if (is_resource($stream)) {
  37. $this->stream = $stream;
  38. } else {
  39. $this->url = $stream;
  40. }
  41. $this->filePermission = $filePermission;
  42. $this->useLocking = $useLocking;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function close()
  48. {
  49. if (is_resource($this->stream)) {
  50. fclose($this->stream);
  51. }
  52. $this->stream = null;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function write(array $record)
  58. {
  59. if (!is_resource($this->stream)) {
  60. if (!$this->url) {
  61. throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
  62. }
  63. $this->errorMessage = null;
  64. set_error_handler(array($this, 'customErrorHandler'));
  65. $this->stream = fopen($this->url, 'a');
  66. if ($this->filePermission !== null) {
  67. @chmod($this->url, $this->filePermission);
  68. }
  69. restore_error_handler();
  70. if (!is_resource($this->stream)) {
  71. $this->stream = null;
  72. throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
  73. }
  74. }
  75. if ($this->useLocking) {
  76. // ignoring errors here, there's not much we can do about them
  77. flock($this->stream, LOCK_EX);
  78. }
  79. fwrite($this->stream, (string) $record['formatted']);
  80. if ($this->useLocking) {
  81. flock($this->stream, LOCK_UN);
  82. }
  83. }
  84. private function customErrorHandler($code, $msg)
  85. {
  86. $this->errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg);
  87. }
  88. }