PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Log/Writer/Stream.php

https://github.com/pborreli/zf2
PHP | 151 lines | 77 code | 17 blank | 57 comment | 13 complexity | 6b5051969d5a32fd10ffdb1143084b41 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Log
  9. */
  10. namespace Zend\Log\Writer;
  11. use Traversable;
  12. use Zend\Log\Exception;
  13. use Zend\Log\Formatter\Simple as SimpleFormatter;
  14. use Zend\Stdlib\ErrorHandler;
  15. /**
  16. * @category Zend
  17. * @package Zend_Log
  18. * @subpackage Writer
  19. */
  20. class Stream extends AbstractWriter
  21. {
  22. /**
  23. * Separator between log entries
  24. *
  25. * @var string
  26. */
  27. protected $logSeparator = PHP_EOL;
  28. /**
  29. * Holds the PHP stream to log to.
  30. *
  31. * @var null|stream
  32. */
  33. protected $stream = null;
  34. /**
  35. * Constructor
  36. *
  37. * @param string|resource|array|Traversable $streamOrUrl Stream or URL to open as a stream
  38. * @param string|null $mode Mode, only applicable if a URL is given
  39. * @param null|string $logSeparator Log separator string
  40. * @return Stream
  41. * @throws Exception\InvalidArgumentException
  42. * @throws Exception\RuntimeException
  43. */
  44. public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
  45. {
  46. if ($streamOrUrl instanceof Traversable) {
  47. $streamOrUrl = iterator_to_array($streamOrUrl);
  48. }
  49. if (is_array($streamOrUrl)) {
  50. $mode = isset($streamOrUrl['mode']) ? $streamOrUrl['mode'] : null;
  51. $logSeparator = isset($streamOrUrl['log_separator']) ? $streamOrUrl['log_separator'] : null;
  52. $streamOrUrl = isset($streamOrUrl['stream']) ? $streamOrUrl['stream'] : null;
  53. }
  54. // Setting the default mode
  55. if (null === $mode) {
  56. $mode = 'a';
  57. }
  58. if (is_resource($streamOrUrl)) {
  59. if ('stream' != get_resource_type($streamOrUrl)) {
  60. throw new Exception\InvalidArgumentException(sprintf(
  61. 'Resource is not a stream; received "%s',
  62. get_resource_type($streamOrUrl)
  63. ));
  64. }
  65. if ('a' != $mode) {
  66. throw new Exception\InvalidArgumentException(sprintf(
  67. 'Mode must be "a" on existing streams; received "%s"',
  68. $mode
  69. ));
  70. }
  71. $this->stream = $streamOrUrl;
  72. } else {
  73. if (!$this->stream = @fopen($streamOrUrl, $mode, false)) {
  74. throw new Exception\RuntimeException(sprintf(
  75. '"%s" cannot be opened with mode "%s"',
  76. $streamOrUrl,
  77. $mode
  78. ));
  79. }
  80. }
  81. if (null !== $logSeparator) {
  82. $this->setLogSeparator($logSeparator);
  83. }
  84. $this->formatter = new SimpleFormatter();
  85. }
  86. /**
  87. * Write a message to the log.
  88. *
  89. * @param array $event event data
  90. * @return void
  91. * @throws Exception\RuntimeException
  92. */
  93. protected function doWrite(array $event)
  94. {
  95. $line = $this->formatter->format($event) . $this->logSeparator;
  96. ErrorHandler::start(E_WARNING);
  97. $result = fwrite($this->stream, $line);
  98. ErrorHandler::stop();
  99. if (false === $result) {
  100. throw new Exception\RuntimeException("Unable to write to stream");
  101. }
  102. }
  103. /**
  104. * Set log separator string
  105. *
  106. * @param string $logSeparator
  107. * @return Stream
  108. */
  109. public function setLogSeparator($logSeparator)
  110. {
  111. $this->logSeparator = (string) $logSeparator;
  112. return $this;
  113. }
  114. /**
  115. * Get log separator string
  116. *
  117. * @return string
  118. */
  119. public function getLogSeparator()
  120. {
  121. return $this->logSeparator;
  122. }
  123. /**
  124. * Close the stream resource.
  125. *
  126. * @return void
  127. */
  128. public function shutdown()
  129. {
  130. if (is_resource($this->stream)) {
  131. fclose($this->stream);
  132. }
  133. }
  134. }