Go to the documentation of this file.00001 <?php
00020 class axLog {
00021
00026 protected $_options;
00027
00032 protected $_first;
00033
00038 protected $_last;
00039
00044 protected $_message_history = array();
00045
00060 public function __construct (array $options = array()) {
00061 $default = array(
00062 'ignore_repeated_messages' => true,
00063 'log_errors' => true,
00064 'log_exception' => true,
00065 );
00066
00067 $this->_options = $options;
00068
00069 if ($this->_options['log_errors'])
00070 $this->registerErrorHandler();
00071
00072 if ($this->_options['log_exception'])
00073 $this->registerExceptionHandler();
00074 }
00075
00084 public function message ($msg, $priority) {
00085 if (!isset($this->_first))
00086 return $this;
00087
00088 if ($this->_options['ignore_repeated_messages'] && array_search($msg, $this->_message_history) !== false)
00089 return $this;
00090
00091 $this->_first->message($this->_message_history[] = $msg, $priority);
00092 return $this;
00093 }
00094
00101 public function error ($msg) {
00102 return $this->message($msg, axLogger::ERR);
00103 }
00104
00111 public function notice ($msg) {
00112 return $this->message($msg, axLogger::NOTICE);
00113 }
00114
00121 public function warning ($msg) {
00122 return $this->message($msg, axLogger::WARNING);
00123 }
00124
00133 public function debug ($msg, array $bt = array()) {
00134 if ($bt)
00135 $msg .= " in {$bt[0]['file']} on line {$bt[0]['line']}";
00136
00137 return $this->message($msg, axLogger::DEBUG);
00138 }
00139
00145 public function addLogger (axLogger $logger) {
00146 if (!isset($this->_first))
00147 $this->_first = $this->_last = $logger;
00148 else
00149 $this->_last->setNext($this->_last = $logger);
00150 return $this;
00151 }
00152
00158 public function registerErrorHandler ($error_types = -1) {
00159 return set_error_handler(array($this, 'handleError'));
00160 }
00161
00166 public function restoreErrorHandler () {
00167 return restore_error_handler();
00168 }
00169
00179 public function handleError ($errno, $errstr, $errfile, $errline) {
00180 $error = "(PHP Error) $errstr in $errfile on line $errline";
00181 switch ($errno) {
00182 case E_STRICT:
00183 case E_WARNING:
00184 case E_USER_WARNING:
00185 $this->warning($error);
00186 break;
00187
00188 case E_NOTICE:
00189 case E_USER_NOTICE:
00190 $this->notice($error);
00191 break;
00192
00193 case E_USER_ERROR:
00194 $this->error($error);
00195 break;
00196
00197 default:
00198 case E_RECOVERABLE_ERROR:
00199 throw new ErrorException($errstr, 2048, $errno, $errfile, $errline);
00200 break;
00201 }
00202 }
00203
00208 public function registerExceptionHandler () {
00209 return set_exception_handler(array($this, 'handleException'));
00210 }
00211
00216 public function restoreExceptionHandler () {
00217 return restore_exception_handler();
00218 }
00219
00229 public function handleException (Exception $exception) {
00230 if (PHP_VERSION_ID >= 50300) {
00231 if ($previous = $exception->getPrevious()) {
00232 $this->handleException($previous);
00233 }
00234 }
00235
00236 $error = "(PHP Exception) " . $exception->getMessage() . ' in ' . $exception->getFile() . ' on line ' .
00237 $exception->getLine();
00238 $this->error($error);
00239 }
00240
00245 public function getHistory () {
00246 return $this->_message_history;
00247 }
00248 }
00249