00001 <?php
00029 class Zend_Log
00030 {
00031 const EMERG = 0;
00032 const ALERT = 1;
00033 const CRIT = 2;
00034 const ERR = 3;
00035 const WARN = 4;
00036 const NOTICE = 5;
00037 const INFO = 6;
00038 const DEBUG = 7;
00039
00044 protected $_priorities = array();
00045
00049 protected $_writers = array();
00050
00054 protected $_filters = array();
00055
00059 protected $_extras = array();
00060
00066 public function __construct(Zend_Log_Writer_Abstract $writer = null)
00067 {
00068 $r = new ReflectionClass($this);
00069 $this->_priorities = array_flip($r->getConstants());
00070
00071 if ($writer !== null) {
00072 $this->addWriter($writer);
00073 }
00074 }
00075
00081 public function __destruct()
00082 {
00083 foreach($this->_writers as $writer) {
00084 $writer->shutdown();
00085 }
00086 }
00087
00099 public function __call($method, $params)
00100 {
00101 $priority = strtoupper($method);
00102 if (($priority = array_search($priority, $this->_priorities)) !== false) {
00103 $this->log(array_shift($params), $priority);
00104 } else {
00106 require_once 'Zend/Log/Exception.php';
00107 throw new Zend_Log_Exception('Bad log priority');
00108 }
00109 }
00110
00119 public function log($message, $priority)
00120 {
00121
00122 if (empty($this->_writers)) {
00124 require_once 'Zend/Log/Exception.php';
00125 throw new Zend_Log_Exception('No writers were added');
00126 }
00127
00128 if (! isset($this->_priorities[$priority])) {
00130 require_once 'Zend/Log/Exception.php';
00131 throw new Zend_Log_Exception('Bad log priority');
00132 }
00133
00134
00135 $event = array_merge(array('timestamp' => date('c'),
00136 'message' => $message,
00137 'priority' => $priority,
00138 'priorityName' => $this->_priorities[$priority]),
00139 $this->_extras);
00140
00141
00142 foreach ($this->_filters as $filter) {
00143 if (! $filter->accept($event)) {
00144 return;
00145 }
00146 }
00147
00148
00149 foreach ($this->_writers as $writer) {
00150 $writer->write($event);
00151 }
00152 }
00153
00161 public function addPriority($name, $priority)
00162 {
00163
00164 $name = strtoupper($name);
00165
00166 if (isset($this->_priorities[$priority])
00167 || array_search($name, $this->_priorities)) {
00169 require_once 'Zend/Log/Exception.php';
00170 throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
00171 }
00172
00173 $this->_priorities[$priority] = $name;
00174 }
00175
00184 public function addFilter($filter)
00185 {
00186 if (is_integer($filter)) {
00188 require_once 'Zend/Log/Filter/Priority.php';
00189 $filter = new Zend_Log_Filter_Priority($filter);
00190 } elseif(!is_object($filter) || ! $filter instanceof Zend_Log_Filter_Interface) {
00192 require_once 'Zend/Log/Exception.php';
00193 throw new Zend_Log_Exception('Invalid filter provided');
00194 }
00195
00196 $this->_filters[] = $filter;
00197 }
00198
00206 public function addWriter(Zend_Log_Writer_Abstract $writer)
00207 {
00208 $this->_writers[] = $writer;
00209 }
00210
00218 public function setEventItem($name, $value) {
00219 $this->_extras = array_merge($this->_extras, array($name => $value));
00220 }
00221
00222 }