File indexing completed on 2024-12-22 05:36:50
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Log 0017 * @subpackage Writer 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Log_Writer_Abstract */ 0024 // require_once 'Zend/Log/Writer/Abstract.php'; 0025 0026 /** Zend_Log_Formatter_Simple */ 0027 // require_once 'Zend/Log/Formatter/Simple.php'; 0028 0029 /** 0030 * @category Zend 0031 * @package Zend_Log 0032 * @subpackage Writer 0033 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0034 * @license http://framework.zend.com/license/new-bsd New BSD License 0035 * @version $Id$ 0036 */ 0037 class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract 0038 { 0039 /** 0040 * Holds the PHP stream to log to. 0041 * 0042 * @var null|stream 0043 */ 0044 protected $_stream = null; 0045 0046 /** 0047 * Class Constructor 0048 * 0049 * @param array|string|resource $streamOrUrl Stream or URL to open as a stream 0050 * @param string|null $mode Mode, only applicable if a URL is given 0051 * @return void 0052 * @throws Zend_Log_Exception 0053 */ 0054 public function __construct($streamOrUrl, $mode = null) 0055 { 0056 // Setting the default 0057 if (null === $mode) { 0058 $mode = 'a'; 0059 } 0060 0061 if (is_resource($streamOrUrl)) { 0062 if (get_resource_type($streamOrUrl) != 'stream') { 0063 // require_once 'Zend/Log/Exception.php'; 0064 throw new Zend_Log_Exception('Resource is not a stream'); 0065 } 0066 0067 if ($mode != 'a') { 0068 // require_once 'Zend/Log/Exception.php'; 0069 throw new Zend_Log_Exception('Mode cannot be changed on existing streams'); 0070 } 0071 0072 $this->_stream = $streamOrUrl; 0073 } else { 0074 if (is_array($streamOrUrl) && isset($streamOrUrl['stream'])) { 0075 $streamOrUrl = $streamOrUrl['stream']; 0076 } 0077 0078 if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) { 0079 // require_once 'Zend/Log/Exception.php'; 0080 $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\""; 0081 throw new Zend_Log_Exception($msg); 0082 } 0083 } 0084 0085 $this->_formatter = new Zend_Log_Formatter_Simple(); 0086 } 0087 0088 /** 0089 * Create a new instance of Zend_Log_Writer_Stream 0090 * 0091 * @param array|Zend_Config $config 0092 * @return Zend_Log_Writer_Stream 0093 */ 0094 static public function factory($config) 0095 { 0096 $config = self::_parseConfig($config); 0097 $config = array_merge(array( 0098 'stream' => null, 0099 'mode' => null, 0100 ), $config); 0101 0102 $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream']; 0103 0104 return new self( 0105 $streamOrUrl, 0106 $config['mode'] 0107 ); 0108 } 0109 0110 /** 0111 * Close the stream resource. 0112 * 0113 * @return void 0114 */ 0115 public function shutdown() 0116 { 0117 if (is_resource($this->_stream)) { 0118 fclose($this->_stream); 0119 } 0120 } 0121 0122 /** 0123 * Write a message to the log. 0124 * 0125 * @param array $event event data 0126 * @return void 0127 * @throws Zend_Log_Exception 0128 */ 0129 protected function _write($event) 0130 { 0131 $line = $this->_formatter->format($event); 0132 0133 if (false === @fwrite($this->_stream, $line)) { 0134 // require_once 'Zend/Log/Exception.php'; 0135 throw new Zend_Log_Exception("Unable to write to stream"); 0136 } 0137 } 0138 }