PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/log4php/LoggerLoggingEvent.php

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