|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Stream.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** Zend_Log_Writer_Abstract */ |
|
24 require_once 'Zend/Log/Writer/Abstract.php'; |
|
25 |
|
26 /** Zend_Log_Formatter_Simple */ |
|
27 require_once 'Zend/Log/Formatter/Simple.php'; |
|
28 |
|
29 /** |
|
30 * @category Zend |
|
31 * @package Zend_Log |
|
32 * @subpackage Writer |
|
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
34 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
35 * @version $Id: Stream.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
36 */ |
|
37 class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract |
|
38 { |
|
39 /** |
|
40 * Holds the PHP stream to log to. |
|
41 * @var null|stream |
|
42 */ |
|
43 protected $_stream = null; |
|
44 |
|
45 /** |
|
46 * Class Constructor |
|
47 * |
|
48 * @param streamOrUrl Stream or URL to open as a stream |
|
49 * @param mode Mode, only applicable if a URL is given |
|
50 */ |
|
51 public function __construct($streamOrUrl, $mode = NULL) |
|
52 { |
|
53 // Setting the default |
|
54 if ($mode === NULL) { |
|
55 $mode = 'a'; |
|
56 } |
|
57 |
|
58 if (is_resource($streamOrUrl)) { |
|
59 if (get_resource_type($streamOrUrl) != 'stream') { |
|
60 require_once 'Zend/Log/Exception.php'; |
|
61 throw new Zend_Log_Exception('Resource is not a stream'); |
|
62 } |
|
63 |
|
64 if ($mode != 'a') { |
|
65 require_once 'Zend/Log/Exception.php'; |
|
66 throw new Zend_Log_Exception('Mode cannot be changed on existing streams'); |
|
67 } |
|
68 |
|
69 $this->_stream = $streamOrUrl; |
|
70 } else { |
|
71 if (is_array($streamOrUrl) && isset($streamOrUrl['stream'])) { |
|
72 $streamOrUrl = $streamOrUrl['stream']; |
|
73 } |
|
74 |
|
75 if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) { |
|
76 require_once 'Zend/Log/Exception.php'; |
|
77 $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\""; |
|
78 throw new Zend_Log_Exception($msg); |
|
79 } |
|
80 } |
|
81 |
|
82 $this->_formatter = new Zend_Log_Formatter_Simple(); |
|
83 } |
|
84 |
|
85 /** |
|
86 * Create a new instance of Zend_Log_Writer_Mock |
|
87 * |
|
88 * @param array|Zend_Config $config |
|
89 * @return Zend_Log_Writer_Mock |
|
90 * @throws Zend_Log_Exception |
|
91 */ |
|
92 static public function factory($config) |
|
93 { |
|
94 $config = self::_parseConfig($config); |
|
95 $config = array_merge(array( |
|
96 'stream' => null, |
|
97 'mode' => null, |
|
98 ), $config); |
|
99 |
|
100 $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream']; |
|
101 |
|
102 return new self( |
|
103 $streamOrUrl, |
|
104 $config['mode'] |
|
105 ); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Close the stream resource. |
|
110 * |
|
111 * @return void |
|
112 */ |
|
113 public function shutdown() |
|
114 { |
|
115 if (is_resource($this->_stream)) { |
|
116 fclose($this->_stream); |
|
117 } |
|
118 } |
|
119 |
|
120 /** |
|
121 * Write a message to the log. |
|
122 * |
|
123 * @param array $event event data |
|
124 * @return void |
|
125 */ |
|
126 protected function _write($event) |
|
127 { |
|
128 $line = $this->_formatter->format($event); |
|
129 |
|
130 if (false === @fwrite($this->_stream, $line)) { |
|
131 require_once 'Zend/Log/Exception.php'; |
|
132 throw new Zend_Log_Exception("Unable to write to stream"); |
|
133 } |
|
134 } |
|
135 } |