12 * obtain it through the world-wide-web, please send an email |
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. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Log |
16 * @package Zend_Log |
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @version $Id: Log.php 22976 2010-09-19 11:57:26Z intiilapa $ |
19 * @version $Id: Log.php 25131 2012-11-16 15:29:18Z rob $ |
20 */ |
20 */ |
21 |
21 |
22 /** |
22 /** |
23 * @category Zend |
23 * @category Zend |
24 * @package Zend_Log |
24 * @package Zend_Log |
25 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
25 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
26 * @license http://framework.zend.com/license/new-bsd New BSD License |
26 * @license http://framework.zend.com/license/new-bsd New BSD License |
27 * @version $Id: Log.php 22976 2010-09-19 11:57:26Z intiilapa $ |
27 * @version $Id: Log.php 25131 2012-11-16 15:29:18Z rob $ |
28 */ |
28 */ |
29 class Zend_Log |
29 class Zend_Log |
30 { |
30 { |
31 const EMERG = 0; // Emergency: system is unusable |
31 const EMERG = 0; // Emergency: system is unusable |
32 const ALERT = 1; // Alert: action must be taken immediately |
32 const ALERT = 1; // Alert: action must be taken immediately |
113 * Factory to construct the logger and one or more writers |
120 * Factory to construct the logger and one or more writers |
114 * based on the configuration array |
121 * based on the configuration array |
115 * |
122 * |
116 * @param array|Zend_Config Array or instance of Zend_Config |
123 * @param array|Zend_Config Array or instance of Zend_Config |
117 * @return Zend_Log |
124 * @return Zend_Log |
|
125 * @throws Zend_Log_Exception |
118 */ |
126 */ |
119 static public function factory($config = array()) |
127 static public function factory($config = array()) |
120 { |
128 { |
121 if ($config instanceof Zend_Config) { |
129 if ($config instanceof Zend_Config) { |
122 $config = $config->toArray(); |
130 $config = $config->toArray(); |
127 require_once 'Zend/Log/Exception.php'; |
135 require_once 'Zend/Log/Exception.php'; |
128 throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config'); |
136 throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config'); |
129 } |
137 } |
130 |
138 |
131 $log = new self; |
139 $log = new self; |
|
140 |
|
141 if (array_key_exists('timestampFormat', $config)) { |
|
142 if (null != $config['timestampFormat'] && '' != $config['timestampFormat']) { |
|
143 $log->setTimestampFormat($config['timestampFormat']); |
|
144 } |
|
145 unset($config['timestampFormat']); |
|
146 } |
132 |
147 |
133 if (!is_array(current($config))) { |
148 if (!is_array(current($config))) { |
134 $log->addWriter(current($config)); |
149 $log->addWriter(current($config)); |
135 } else { |
150 } else { |
136 foreach($config as $writer) { |
151 foreach($config as $writer) { |
145 /** |
160 /** |
146 * Construct a writer object based on a configuration array |
161 * Construct a writer object based on a configuration array |
147 * |
162 * |
148 * @param array $spec config array with writer spec |
163 * @param array $spec config array with writer spec |
149 * @return Zend_Log_Writer_Abstract |
164 * @return Zend_Log_Writer_Abstract |
|
165 * @throws Zend_Log_Exception |
150 */ |
166 */ |
151 protected function _constructWriterFromConfig($config) |
167 protected function _constructWriterFromConfig($config) |
152 { |
168 { |
153 $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace); |
169 $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace); |
154 |
170 |
164 if (isset($config['filterName'])) { |
180 if (isset($config['filterName'])) { |
165 $filter = $this->_constructFilterFromConfig($config); |
181 $filter = $this->_constructFilterFromConfig($config); |
166 $writer->addFilter($filter); |
182 $writer->addFilter($filter); |
167 } |
183 } |
168 |
184 |
|
185 if (isset($config['formatterName'])) { |
|
186 $formatter = $this->_constructFormatterFromConfig($config); |
|
187 $writer->setFormatter($formatter); |
|
188 } |
|
189 |
169 return $writer; |
190 return $writer; |
170 } |
191 } |
171 |
192 |
172 /** |
193 /** |
173 * Construct filter object from configuration array or Zend_Config object |
194 * Construct filter object from configuration array or Zend_Config object |
174 * |
195 * |
175 * @param array|Zend_Config $config Zend_Config or Array |
196 * @param array|Zend_Config $config Zend_Config or Array |
176 * @return Zend_Log_Filter_Interface |
197 * @return Zend_Log_Filter_Interface |
|
198 * @throws Zend_Log_Exception |
177 */ |
199 */ |
178 protected function _constructFilterFromConfig($config) |
200 protected function _constructFilterFromConfig($config) |
179 { |
201 { |
180 $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace); |
202 $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace); |
181 |
203 |
189 } |
211 } |
190 |
212 |
191 return $filter; |
213 return $filter; |
192 } |
214 } |
193 |
215 |
|
216 /** |
|
217 * Construct formatter object from configuration array or Zend_Config object |
|
218 * |
|
219 * @param array|Zend_Config $config Zend_Config or Array |
|
220 * @return Zend_Log_Formatter_Interface |
|
221 * @throws Zend_Log_Exception |
|
222 */ |
|
223 protected function _constructFormatterFromConfig($config) |
|
224 { |
|
225 $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace); |
|
226 |
|
227 if (!$formatter instanceof Zend_Log_Formatter_Interface) { |
|
228 $formatterName = is_object($formatter) |
|
229 ? get_class($formatter) |
|
230 : 'The specified formatter'; |
|
231 /** @see Zend_Log_Exception */ |
|
232 require_once 'Zend/Log/Exception.php'; |
|
233 throw new Zend_Log_Exception($formatterName . ' does not implement Zend_Log_Formatter_Interface'); |
|
234 } |
|
235 |
|
236 return $formatter; |
|
237 } |
|
238 |
194 /** |
239 /** |
195 * Construct a filter or writer from config |
240 * Construct a filter or writer from config |
196 * |
241 * |
197 * @param string $type 'writer' of 'filter' |
242 * @param string $type 'writer' of 'filter' |
198 * @param mixed $config Zend_Config or Array |
243 * @param mixed $config Zend_Config or Array |
199 * @param string $namespace |
244 * @param string $namespace |
200 * @return object |
245 * @return object |
|
246 * @throws Zend_Log_Exception |
201 */ |
247 */ |
202 protected function _constructFromConfig($type, $config, $namespace) |
248 protected function _constructFromConfig($type, $config, $namespace) |
203 { |
249 { |
204 if ($config instanceof Zend_Config) { |
250 if ($config instanceof Zend_Config) { |
205 $config = $config->toArray(); |
251 $config = $config->toArray(); |
221 |
267 |
222 $reflection = new ReflectionClass($className); |
268 $reflection = new ReflectionClass($className); |
223 if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) { |
269 if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) { |
224 require_once 'Zend/Log/Exception.php'; |
270 require_once 'Zend/Log/Exception.php'; |
225 throw new Zend_Log_Exception( |
271 throw new Zend_Log_Exception( |
226 'Driver does not implement Zend_Log_FactoryInterface and can not be constructed from config.' |
272 $className . ' does not implement Zend_Log_FactoryInterface and can not be constructed from config.' |
227 ); |
273 ); |
228 } |
274 } |
229 |
275 |
230 return call_user_func(array($className, 'factory'), $params); |
276 return call_user_func(array($className, 'factory'), $params); |
231 } |
277 } |
235 * |
281 * |
236 * @param array $config |
282 * @param array $config |
237 * @param string $type filter|writer |
283 * @param string $type filter|writer |
238 * @param string $defaultNamespace |
284 * @param string $defaultNamespace |
239 * @return string full classname |
285 * @return string full classname |
|
286 * @throws Zend_Log_Exception |
240 */ |
287 */ |
241 protected function getClassName($config, $type, $defaultNamespace) |
288 protected function getClassName($config, $type, $defaultNamespace) |
242 { |
289 { |
243 if (!isset($config[ $type . 'Name' ])) { |
290 if (!isset($config[$type . 'Name'])) { |
244 require_once 'Zend/Log/Exception.php'; |
291 require_once 'Zend/Log/Exception.php'; |
245 throw new Zend_Log_Exception("Specify {$type}Name in the configuration array"); |
292 throw new Zend_Log_Exception("Specify {$type}Name in the configuration array"); |
246 } |
293 } |
247 $className = $config[ $type . 'Name' ]; |
294 |
248 |
295 $className = $config[$type . 'Name']; |
249 $namespace = $defaultNamespace; |
296 $namespace = $defaultNamespace; |
250 if (isset($config[ $type . 'Namespace' ])) { |
297 |
251 $namespace = $config[ $type . 'Namespace' ]; |
298 if (isset($config[$type . 'Namespace'])) { |
252 } |
299 $namespace = $config[$type . 'Namespace']; |
253 |
300 } |
254 $fullClassName = $namespace . '_' . $className; |
301 |
255 return $fullClassName; |
302 // PHP >= 5.3.0 namespace given? |
|
303 if (substr($namespace, -1) == '\\') { |
|
304 return $namespace . $className; |
|
305 } |
|
306 |
|
307 // emtpy namespace given? |
|
308 if (strlen($namespace) === 0) { |
|
309 return $className; |
|
310 } |
|
311 |
|
312 return $namespace . '_' . $className; |
256 } |
313 } |
257 |
314 |
258 /** |
315 /** |
259 * Packs message and priority into Event array |
316 * Packs message and priority into Event array |
260 * |
317 * |
261 * @param string $message Message to log |
318 * @param string $message Message to log |
262 * @param integer $priority Priority of message |
319 * @param integer $priority Priority of message |
263 * @return array Event array |
320 * @return array Event array |
264 **/ |
321 */ |
265 protected function _packEvent($message, $priority) |
322 protected function _packEvent($message, $priority) |
266 { |
323 { |
267 return array_merge(array( |
324 return array_merge(array( |
268 'timestamp' => date($this->_timestampFormat), |
325 'timestamp' => date($this->_timestampFormat), |
269 'message' => $message, |
326 'message' => $message, |
385 /** |
442 /** |
386 * Add a custom priority |
443 * Add a custom priority |
387 * |
444 * |
388 * @param string $name Name of priority |
445 * @param string $name Name of priority |
389 * @param integer $priority Numeric priority |
446 * @param integer $priority Numeric priority |
390 * @throws Zend_Log_InvalidArgumentException |
447 * @throws Zend_Log_Exception |
391 */ |
448 */ |
392 public function addPriority($name, $priority) |
449 public function addPriority($name, $priority) |
393 { |
450 { |
394 // Priority names must be uppercase for predictability. |
451 // Priority names must be uppercase for predictability. |
395 $name = strtoupper($name); |
452 $name = strtoupper($name); |
408 /** |
465 /** |
409 * Add a filter that will be applied before all log writers. |
466 * Add a filter that will be applied before all log writers. |
410 * Before a message will be received by any of the writers, it |
467 * Before a message will be received by any of the writers, it |
411 * must be accepted by all filters added with this method. |
468 * must be accepted by all filters added with this method. |
412 * |
469 * |
413 * @param int|Zend_Log_Filter_Interface $filter |
470 * @param int|Zend_Config|array|Zend_Log_Filter_Interface $filter |
414 * @return void |
471 * @return Zend_Log |
|
472 * @throws Zend_Log_Exception |
415 */ |
473 */ |
416 public function addFilter($filter) |
474 public function addFilter($filter) |
417 { |
475 { |
418 if (is_integer($filter)) { |
476 if (is_int($filter)) { |
419 /** @see Zend_Log_Filter_Priority */ |
477 /** @see Zend_Log_Filter_Priority */ |
420 require_once 'Zend/Log/Filter/Priority.php'; |
478 require_once 'Zend/Log/Filter/Priority.php'; |
421 $filter = new Zend_Log_Filter_Priority($filter); |
479 $filter = new Zend_Log_Filter_Priority($filter); |
422 |
480 |
423 } elseif ($filter instanceof Zend_Config || is_array($filter)) { |
481 } elseif ($filter instanceof Zend_Config || is_array($filter)) { |
436 /** |
494 /** |
437 * Add a writer. A writer is responsible for taking a log |
495 * Add a writer. A writer is responsible for taking a log |
438 * message and writing it out to storage. |
496 * message and writing it out to storage. |
439 * |
497 * |
440 * @param mixed $writer Zend_Log_Writer_Abstract or Config array |
498 * @param mixed $writer Zend_Log_Writer_Abstract or Config array |
441 * @return void |
499 * @return Zend_Log |
442 */ |
500 */ |
443 public function addWriter($writer) |
501 public function addWriter($writer) |
444 { |
502 { |
445 if (is_array($writer) || $writer instanceof Zend_Config) { |
503 if (is_array($writer) || $writer instanceof Zend_Config) { |
446 $writer = $this->_constructWriterFromConfig($writer); |
504 $writer = $this->_constructWriterFromConfig($writer); |
460 } |
518 } |
461 |
519 |
462 /** |
520 /** |
463 * Set an extra item to pass to the log writers. |
521 * Set an extra item to pass to the log writers. |
464 * |
522 * |
465 * @param $name Name of the field |
523 * @param string $name Name of the field |
466 * @param $value Value of the field |
524 * @param string $value Value of the field |
467 * @return void |
525 * @return Zend_Log |
468 */ |
526 */ |
469 public function setEventItem($name, $value) |
527 public function setEventItem($name, $value) |
470 { |
528 { |
471 $this->_extras = array_merge($this->_extras, array($name => $value)); |
529 $this->_extras = array_merge($this->_extras, array($name => $value)); |
472 return $this; |
530 return $this; |
489 */ |
547 */ |
490 public function registerErrorHandler() |
548 public function registerErrorHandler() |
491 { |
549 { |
492 // Only register once. Avoids loop issues if it gets registered twice. |
550 // Only register once. Avoids loop issues if it gets registered twice. |
493 if ($this->_registeredErrorHandler) { |
551 if ($this->_registeredErrorHandler) { |
494 return $this; |
552 return $this; |
495 } |
553 } |
496 |
554 |
497 $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler')); |
555 $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler')); |
498 |
556 |
499 // Contruct a default map of phpErrors to Zend_Log priorities. |
557 // Contruct a default map of phpErrors to Zend_Log priorities. |
535 */ |
593 */ |
536 public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) |
594 public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) |
537 { |
595 { |
538 $errorLevel = error_reporting(); |
596 $errorLevel = error_reporting(); |
539 |
597 |
540 if ($errorLevel && $errno) { |
598 if ($errorLevel & $errno) { |
541 if (isset($this->_errorHandlerMap[$errno])) { |
599 if (isset($this->_errorHandlerMap[$errno])) { |
542 $priority = $this->_errorHandlerMap[$errno]; |
600 $priority = $this->_errorHandlerMap[$errno]; |
543 } else { |
601 } else { |
544 $priority = Zend_Log::INFO; |
602 $priority = Zend_Log::INFO; |
545 } |
603 } |