Stream.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Log_Writer_Abstract */
  23. require_once 'Zend/Log/Writer/Abstract.php';
  24. /** Zend_Log_Formatter_Simple */
  25. require_once 'Zend/Log/Formatter/Simple.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage Writer
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @version $Id$
  33. */
  34. class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract
  35. {
  36. /**
  37. * Holds the PHP stream to log to.
  38. * @var null|stream
  39. */
  40. protected $_stream = null;
  41. /**
  42. * Class Constructor
  43. *
  44. * @param streamOrUrl Stream or URL to open as a stream
  45. * @param mode Mode, only applicable if a URL is given
  46. */
  47. public function __construct($streamOrUrl, $mode = 'a')
  48. {
  49. if (is_resource($streamOrUrl)) {
  50. if (get_resource_type($streamOrUrl) != 'stream') {
  51. require_once 'Zend/Log/Exception.php';
  52. throw new Zend_Log_Exception('Resource is not a stream');
  53. }
  54. if ($mode != 'a') {
  55. require_once 'Zend/Log/Exception.php';
  56. throw new Zend_Log_Exception('Mode cannot be changed on existing streams');
  57. }
  58. $this->_stream = $streamOrUrl;
  59. } else {
  60. if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) {
  61. require_once 'Zend/Log/Exception.php';
  62. $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\"";
  63. throw new Zend_Log_Exception($msg);
  64. }
  65. }
  66. $this->_formatter = new Zend_Log_Formatter_Simple();
  67. }
  68. /**
  69. * Close the stream resource.
  70. *
  71. * @return void
  72. */
  73. public function shutdown()
  74. {
  75. if (is_resource($this->_stream)) {
  76. fclose($this->_stream);
  77. }
  78. }
  79. /**
  80. * Write a message to the log.
  81. *
  82. * @param array $event event data
  83. * @return void
  84. */
  85. protected function _write($event)
  86. {
  87. $line = $this->_formatter->format($event);
  88. if (false === @fwrite($this->_stream, $line)) {
  89. require_once 'Zend/Log/Exception.php';
  90. throw new Zend_Log_Exception("Unable to write to stream");
  91. }
  92. }
  93. }