diff --git a/library/log4php/LoggerLoggingEvent.php b/library/log4php/LoggerLoggingEvent.php new file mode 100644 --- /dev/null +++ b/library/log4php/LoggerLoggingEvent.php @@ -0,0 +1,370 @@ +fqcn = $fqcn; + if ($logger instanceof Logger) { + $this->logger = $logger; + $this->categoryName = $logger->getName(); + } else { + $this->categoryName = strval($logger); + } + $this->level = $level; + $this->message = $message; + if ($timeStamp !== null && is_numeric($timeStamp)) { + $this->timeStamp = $timeStamp; + } else { + $this->timeStamp = microtime(true); + } + + if ($throwable !== null && $throwable instanceof Exception) { + $this->throwableInfo = new LoggerThrowableInformation($throwable); + } + } + + /** + * Returns the full qualified classname. + * TODO: PHP does contain namespaces in 5.3. Those should be returned too, + */ + public function getFullQualifiedClassname() { + return $this->fqcn; + } + + /** + * Set the location information for this logging event. The collected + * information is cached for future use. + * + *

This method uses {@link PHP_MANUAL#debug_backtrace debug_backtrace()} function (if exists) + * to collect informations about caller.

+ *

It only recognize informations generated by {@link Logger} and its subclasses.

+ * @return LoggerLocationInfo + */ + public function getLocationInformation() { + if ($this->locationInfo === null) { + + $locationInfo = array(); + $trace = debug_backtrace(); + $prevHop = null; + // make a downsearch to identify the caller + $hop = array_pop($trace); + while ($hop !== null) { + if (isset($hop['class'])) { + // we are sometimes in functions = no class available: avoid php warning here + $className = strtolower($hop['class']); + if (!empty($className) and ($className == 'logger' or + strtolower(get_parent_class($className)) == 'logger') + ) { + $locationInfo['line'] = $hop['line']; + $locationInfo['file'] = $hop['file']; + break; + } + } + $prevHop = $hop; + $hop = array_pop($trace); + } + $locationInfo['class'] = isset($prevHop['class']) ? $prevHop['class'] : 'main'; + if (isset($prevHop['function']) and + $prevHop['function'] !== 'include' and + $prevHop['function'] !== 'include_once' and + $prevHop['function'] !== 'require' and + $prevHop['function'] !== 'require_once' + ) { + + $locationInfo['function'] = $prevHop['function']; + } else { + $locationInfo['function'] = 'main'; + } + + $this->locationInfo = new LoggerLocationInfo($locationInfo, $this->fqcn); + } + return $this->locationInfo; + } + + /** + * Return the level of this event. Use this form instead of directly + * accessing the {@link $level} field. + * @return LoggerLevel + */ + public function getLevel() { + return $this->level; + } + + /** + * Returns the logger which created the event. + * @return Logger + */ + public function getLogger() { + return $this->logger; + } + + /** + * Return the name of the logger. Use this form instead of directly + * accessing the {@link $categoryName} field. + * @return string + */ + public function getLoggerName() { + return $this->categoryName; + } + + /** + * Return the message for this logging event. + * @return mixed + */ + public function getMessage() { + return $this->message; + } + + /** + * This method returns the NDC for this event. It will return the + * correct content even if the event was generated in a different + * thread or even on a different machine. The {@link LoggerNDC::get()} method + * should never be called directly. + * @return string + */ + public function getNDC() { + if ($this->ndcLookupRequired) { + $this->ndcLookupRequired = false; + $this->ndc = LoggerNDC::get(); + } + return $this->ndc; + } + + /** + * Returns the the context corresponding to the key + * parameter. + * @return string + */ + public function getMDC($key) { + return LoggerMDC::get($key); + } + + /** + * Returns the entire MDC context. + * @return array + */ + public function getMDCMap() { + return LoggerMDC::getMap(); + } + + /** + * Render message. + * @return string + */ + public function getRenderedMessage() { + if ($this->renderedMessage === null and $this->message !== null) { + if (is_string($this->message)) { + $this->renderedMessage = $this->message; + } else { + $rendererMap = Logger::getHierarchy()->getRendererMap(); + $this->renderedMessage = $rendererMap->findAndRender($this->message); + } + } + return $this->renderedMessage; + } + + /** + * Returns the time when the application started, as a UNIX timestamp + * with microseconds. + * @return float + */ + public static function getStartTime() { + if (!isset(self::$startTime)) { + self::$startTime = microtime(true); + } + return self::$startTime; + } + + /** + * @return float + */ + public function getTimeStamp() { + return $this->timeStamp; + } + + /** + * Returns the time in seconds passed from the beginning of execution to + * the time the event was constructed. + * + * @return float Seconds with microseconds in decimals. + */ + public function getRelativeTime() { + return $this->timeStamp - self::$startTime; + } + + /** + * Returns the time in milliseconds passed from the beginning of execution + * to the time the event was constructed. + * + * @deprecated This method has been replaced by getRelativeTime which + * does not perform unneccesary multiplication and formatting. + * + * @return integer + */ + public function getTime() { + $eventTime = $this->getTimeStamp(); + $eventStartTime = LoggerLoggingEvent::getStartTime(); + return number_format(($eventTime - $eventStartTime) * 1000, 0, '', ''); + } + + /** + * @return mixed + */ + public function getThreadName() { + if ($this->threadName === null) { + $this->threadName = (string)getmypid(); + } + return $this->threadName; + } + + /** + * @return mixed LoggerThrowableInformation + */ + public function getThrowableInformation() { + return $this->throwableInfo; + } + + /** + * Serialize this object + * @return string + */ + public function toString() { + serialize($this); + } + + /** + * Avoid serialization of the {@link $logger} object + */ + public function __sleep() { + return array( + 'fqcn', + 'categoryName', + 'level', + 'ndc', + 'ndcLookupRequired', + 'message', + 'renderedMessage', + 'threadName', + 'timeStamp', + 'locationInfo', + ); + } + +} + +LoggerLoggingEvent::getStartTime();