PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/MyVenture-BlackHawk/Library/Logging/Include/LoggerLoggingEvent.php

https://github.com/samar-tmr/MyVenture
PHP | 378 lines | 159 code | 38 blank | 181 comment | 29 complexity | f95a636d13a433de975e5323e115f78f MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @package log4php
  19. */
  20. /**
  21. * The internal representation of logging event.
  22. *
  23. * @version $Revision: 1059292 $
  24. * @package log4php
  25. */
  26. class LoggerLoggingEvent {
  27. private static $startTime;
  28. /**
  29. * @var string Fully Qualified Class Name of the calling category class.
  30. */
  31. private $fqcn;
  32. /**
  33. * @var Logger reference
  34. */
  35. private $logger = null;
  36. /**
  37. * The category (logger) name.
  38. * This field will be marked as private in future
  39. * releases. Please do not access it directly.
  40. * Use the {@link getLoggerName()} method instead.
  41. * @deprecated
  42. */
  43. private $categoryName;
  44. /**
  45. * Level of logging event.
  46. * <p> This field should not be accessed directly. You shoud use the
  47. * {@link getLevel()} method instead.
  48. *
  49. * @deprecated
  50. * @var LoggerLevel
  51. */
  52. protected $level;
  53. /**
  54. * @var string The nested diagnostic context (NDC) of logging event.
  55. */
  56. private $ndc;
  57. /**
  58. * Have we tried to do an NDC lookup? If we did, there is no need
  59. * to do it again. Note that its value is always false when
  60. * serialized. Thus, a receiving SocketNode will never use it's own
  61. * (incorrect) NDC. See also writeObject method.
  62. * @var boolean
  63. */
  64. private $ndcLookupRequired = true;
  65. /**
  66. * Have we tried to do an MDC lookup? If we did, there is no need
  67. * to do it again. Note that its value is always false when
  68. * serialized. See also the getMDC and getMDCCopy methods.
  69. * @var boolean
  70. */
  71. private $mdcCopyLookupRequired = true;
  72. /**
  73. * @var mixed The application supplied message of logging event.
  74. */
  75. private $message;
  76. /**
  77. * The application supplied message rendered through the log4php
  78. * objet rendering mechanism. At present renderedMessage == message.
  79. * @var string
  80. */
  81. private $renderedMessage = null;
  82. /**
  83. * The name of thread in which this logging event was generated.
  84. * log4php saves here the process id via {@link PHP_MANUAL#getmypid getmypid()}
  85. * @var mixed
  86. */
  87. private $threadName = null;
  88. /**
  89. * The number of seconds elapsed from 1/1/1970 until logging event
  90. * was created plus microseconds if available.
  91. * @var float
  92. */
  93. public $timeStamp;
  94. /**
  95. * @var LoggerLocationInfo Location information for the caller.
  96. */
  97. private $locationInfo = null;
  98. /**
  99. * @var LoggerThrowableInformation log4php internal representation of throwable
  100. */
  101. private $throwableInfo = null;
  102. /**
  103. * Instantiate a LoggingEvent from the supplied parameters.
  104. *
  105. * <p>Except {@link $timeStamp} all the other fields of
  106. * LoggerLoggingEvent are filled when actually needed.
  107. *
  108. * @param string $fqcn name of the caller class.
  109. * @param mixed $logger The {@link Logger} category of this event or the logger name.
  110. * @param LoggerLevel $priority The level of this event.
  111. * @param mixed $message The message of this event.
  112. * @param integer $timeStamp the timestamp of this logging event.
  113. * @param Exception $throwable The throwable associated with logging event
  114. */
  115. public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null, Exception $throwable = null) {
  116. $this->fqcn = $fqcn;
  117. if($logger instanceof Logger) {
  118. $this->logger = $logger;
  119. $this->categoryName = $logger->getName();
  120. } else {
  121. $this->categoryName = strval($logger);
  122. }
  123. $this->level = $priority;
  124. $this->message = $message;
  125. if($timeStamp !== null && is_float($timeStamp)) {
  126. $this->timeStamp = $timeStamp;
  127. } else {
  128. if(function_exists('microtime')) {
  129. // get microtime as float
  130. $this->timeStamp = microtime(true);
  131. } else {
  132. $this->timeStamp = floatval(time());
  133. }
  134. }
  135. if ($throwable !== null && $throwable instanceof Exception) {
  136. $this->throwableInfo = new LoggerThrowableInformation($throwable);
  137. }
  138. }
  139. /**
  140. * Returns the full qualified classname.
  141. * TODO: PHP does contain namespaces in 5.3. Those should be returned too,
  142. */
  143. public function getFullQualifiedClassname() {
  144. return $this->fqcn;
  145. }
  146. /**
  147. * Set the location information for this logging event. The collected
  148. * information is cached for future use.
  149. *
  150. * <p>This method uses {@link PHP_MANUAL#debug_backtrace debug_backtrace()} function (if exists)
  151. * to collect informations about caller.</p>
  152. * <p>It only recognize informations generated by {@link Logger} and its subclasses.</p>
  153. * @return LoggerLocationInfo
  154. */
  155. public function getLocationInformation() {
  156. if($this->locationInfo === null) {
  157. $locationInfo = array();
  158. if(function_exists('debug_backtrace')) {
  159. $trace = debug_backtrace();
  160. $prevHop = null;
  161. // make a downsearch to identify the caller
  162. $hop = array_pop($trace);
  163. while($hop !== null) {
  164. if(isset($hop['class'])) {
  165. // we are sometimes in functions = no class available: avoid php warning here
  166. $className = strtolower($hop['class']);
  167. if(!empty($className) and ($className == 'logger' or $className == 'loggercategory' or
  168. strtolower(get_parent_class($className)) == 'logger' or
  169. strtolower(get_parent_class($className)) == 'loggercategory')) {
  170. $locationInfo['line'] = $hop['line'];
  171. $locationInfo['file'] = $hop['file'];
  172. break;
  173. }
  174. }
  175. $prevHop = $hop;
  176. $hop = array_pop($trace);
  177. }
  178. $locationInfo['class'] = isset($prevHop['class']) ? $prevHop['class'] : 'main';
  179. if(isset($prevHop['function']) and
  180. $prevHop['function'] !== 'include' and
  181. $prevHop['function'] !== 'include_once' and
  182. $prevHop['function'] !== 'require' and
  183. $prevHop['function'] !== 'require_once') {
  184. $locationInfo['function'] = $prevHop['function'];
  185. } else {
  186. $locationInfo['function'] = 'main';
  187. }
  188. }
  189. $this->locationInfo = new LoggerLocationInfo($locationInfo, $this->fqcn);
  190. }
  191. return $this->locationInfo;
  192. }
  193. /**
  194. * Return the level of this event. Use this form instead of directly
  195. * accessing the {@link $level} field.
  196. * @return LoggerLevel
  197. */
  198. public function getLevel() {
  199. return $this->level;
  200. }
  201. /**
  202. * Return the name of the logger. Use this form instead of directly
  203. * accessing the {@link $categoryName} field.
  204. * @return string
  205. */
  206. public function getLoggerName() {
  207. return $this->categoryName;
  208. }
  209. /**
  210. * Return the message for this logging event.
  211. *
  212. * <p>Before serialization, the returned object is the message
  213. * passed by the user to generate the logging event. After
  214. * serialization, the returned value equals the String form of the
  215. * message possibly after object rendering.
  216. * @return mixed
  217. */
  218. public function getMessage() {
  219. if($this->message !== null) {
  220. return $this->message;
  221. } else {
  222. return $this->getRenderedMessage();
  223. }
  224. }
  225. /**
  226. * This method returns the NDC for this event. It will return the
  227. * correct content even if the event was generated in a different
  228. * thread or even on a different machine. The {@link LoggerNDC::get()} method
  229. * should <b>never</b> be called directly.
  230. * @return string
  231. */
  232. public function getNDC() {
  233. if($this->ndcLookupRequired) {
  234. $this->ndcLookupRequired = false;
  235. $this->ndc = LoggerNDC::get();
  236. }
  237. return $this->ndc;
  238. }
  239. /**
  240. * Returns the the context corresponding to the <code>key</code>
  241. * parameter.
  242. * @return string
  243. */
  244. public function getMDC($key) {
  245. return LoggerMDC::get($key);
  246. }
  247. /**
  248. * Render message.
  249. * @return string
  250. */
  251. public function getRenderedMessage() {
  252. if($this->renderedMessage === null and $this->message !== null) {
  253. if(is_string($this->message)) {
  254. $this->renderedMessage = $this->message;
  255. } else {
  256. // $this->logger might be null or an instance of Logger or RootLogger
  257. // But in contrast to log4j, in log4php there is only have one LoggerHierarchy so there is
  258. // no need figure out which one is $this->logger part of.
  259. // TODO: Logger::getHierarchy() is marked @deprecated!
  260. $repository = Logger::getHierarchy();
  261. $rendererMap = $repository->getRendererMap();
  262. $this->renderedMessage= $rendererMap->findAndRender($this->message);
  263. }
  264. }
  265. return $this->renderedMessage;
  266. }
  267. /**
  268. * Returns the time when the application started, in seconds
  269. * elapsed since 01.01.1970 plus microseconds if available.
  270. *
  271. * @return float
  272. * @static
  273. */
  274. public static function getStartTime() {
  275. if(!isset(self::$startTime)) {
  276. if (function_exists('microtime')) {
  277. // microtime as float
  278. self::$startTime = microtime(true);
  279. } else {
  280. self::$startTime = floatval(time());
  281. }
  282. }
  283. return self::$startTime;
  284. }
  285. /**
  286. * @return float
  287. */
  288. public function getTimeStamp() {
  289. return $this->timeStamp;
  290. }
  291. /**
  292. * Calculates the time of this event.
  293. * @return the time after event starttime when this event has occured
  294. */
  295. public function getTime() {
  296. $eventTime = (float)$this->getTimeStamp();
  297. $eventStartTime = (float)LoggerLoggingEvent::getStartTime();
  298. return number_format(($eventTime - $eventStartTime) * 1000, 0, '', '');
  299. }
  300. /**
  301. * @return mixed
  302. */
  303. public function getThreadName() {
  304. if ($this->threadName === null) {
  305. $this->threadName = (string)getmypid();
  306. }
  307. return $this->threadName;
  308. }
  309. /**
  310. * @return mixed LoggerThrowableInformation
  311. */
  312. public function getThrowableInformation() {
  313. return $this->throwableInfo;
  314. }
  315. /**
  316. * Serialize this object
  317. * @return string
  318. */
  319. public function toString() {
  320. serialize($this);
  321. }
  322. /**
  323. * Avoid serialization of the {@link $logger} object
  324. */
  325. public function __sleep() {
  326. return array(
  327. 'fqcn',
  328. 'categoryName',
  329. 'level',
  330. 'ndc',
  331. 'ndcLookupRequired',
  332. 'message',
  333. 'renderedMessage',
  334. 'threadName',
  335. 'timeStamp',
  336. 'locationInfo',
  337. );
  338. }
  339. }
  340. LoggerLoggingEvent::getStartTime();