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-2015 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 23 /** Zend_Log_Filter_Priority */ 24 25 /** 26 * @category Zend 27 * @package Zend_Log 28 * @subpackage Writer 29 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 30 * @license http://framework.zend.com/license/new-bsd New BSD License 31 * @version $Id$ 32 */ 33 abstract class Zend_Log_Writer_Abstract implements Zend_Log_FactoryInterface 34 { 35 /** 36 * @var array of Zend_Log_Filter_Interface 37 */ 38 protected $_filters = array(); 39 40 /** 41 * Formats the log message before writing. 42 * 43 * @var Zend_Log_Formatter_Interface 44 */ 45 protected $_formatter; 46 47 /** 48 * Add a filter specific to this writer. 49 * 50 * @param Zend_Log_Filter_Interface|int $filter Filter class or filter 51 * priority 52 * @return Zend_Log_Writer_Abstract 53 * @throws Zend_Log_Exception 54 */ 55 public function addFilter($filter) 56 { 57 if (is_int($filter)) { 58 $filter = new Zend_Log_Filter_Priority($filter); 59 } 60 61 if (!$filter instanceof Zend_Log_Filter_Interface) { 62 /** @see Zend_Log_Exception */ 63 throw new Zend_Log_Exception('Invalid filter provided'); 64 } 65 66 $this->_filters[] = $filter; 67 return $this; 68 } 69 70 /** 71 * Log a message to this writer. 72 * 73 * @param array $event log data event 74 * @return void 75 */ 76 public function write($event) 77 { 78 /** @var Zend_Log_Filter_Interface $filter */ 79 foreach ($this->_filters as $filter) { 80 if (!$filter->accept($event)) { 81 return; 82 } 83 } 84 85 // exception occurs on error 86 $this->_write($event); 87 } 88 89 /** 90 * Set a new formatter for this writer 91 * 92 * @param Zend_Log_Formatter_Interface $formatter 93 * @return Zend_Log_Writer_Abstract 94 */ 95 public function setFormatter(Zend_Log_Formatter_Interface $formatter) 96 { 97 $this->_formatter = $formatter; 98 return $this; 99 } 100 101 /** 102 * Perform shutdown activites such as closing open resources 103 * 104 * @return void 105 */ 106 public function shutdown() 107 {} 108 109 /** 110 * Write a message to the log. 111 * 112 * @param array $event log data event 113 * @return void 114 */ 115 abstract protected function _write($event); 116 117 /** 118 * Validate and optionally convert the config to array 119 * 120 * @param array|Zend_Config $config Zend_Config or Array 121 * @return array 122 * @throws Zend_Log_Exception 123 */ 124 static protected function _parseConfig($config) 125 { 126 if ($config instanceof Zend_Config) { 127 $config = $config->toArray(); 128 } 129 130 if (!is_array($config)) { 131 throw new Zend_Log_Exception( 132 'Configuration must be an array or instance of Zend_Config' 133 ); 134 } 135 136 return $config; 137 } 138 }