PageRenderTime 27ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/scalr_newui/app/src/Lib/IO/Logging/log4php/src/main/php/LoggerNDC.php

https://github.com/kennethjiang/Wolke
PHP | 242 lines | 62 code | 24 blank | 156 comment | 7 complexity | 2f168553ecc8a7a7549acd444367c82d 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. * @package log4php
  20. */
  21. /**
  22. * @ignore
  23. */
  24. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
  25. /**
  26. */
  27. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  28. define('LOGGER_NDC_HT_SIZE', 7);
  29. /**
  30. * This is the global repository of NDC stack
  31. */
  32. $GLOBALS['log4php.LoggerNDC.ht'] = array();
  33. /**
  34. * This is the max depth of NDC stack
  35. */
  36. $GLOBALS['log4php.LoggerNDC.maxDepth'] = LOGGER_NDC_HT_SIZE;
  37. /**
  38. * The NDC class implements <i>nested diagnostic contexts</i> as
  39. * defined by Neil Harrison in the article "Patterns for Logging
  40. * Diagnostic Messages" part of the book "<i>Pattern Languages of
  41. * Program Design 3</i>" edited by Martin et al.
  42. *
  43. * <p>A Nested Diagnostic Context, or NDC in short, is an instrument
  44. * to distinguish interleaved log output from different sources. Log
  45. * output is typically interleaved when a server handles multiple
  46. * clients near-simultaneously.
  47. *
  48. * <p>Interleaved log output can still be meaningful if each log entry
  49. * from different contexts had a distinctive stamp. This is where NDCs
  50. * come into play.
  51. *
  52. * <p><i><b>Note that NDCs are managed on a per thread
  53. * basis</b></i>. NDC operations such as {@link push()}, {@link pop()},
  54. * {@link clear()}, {@link getDepth()} and {@link setMaxDepth()}
  55. * affect the NDC of the <i>current</i> thread only. NDCs of other
  56. * threads remain unaffected.
  57. *
  58. * <p>For example, a servlet can build a per client request NDC
  59. * consisting the clients host name and other information contained in
  60. * the the request. <i>Cookies</i> are another source of distinctive
  61. * information. To build an NDC one uses the {@link push()}
  62. * operation.</p>
  63. *
  64. * Simply put,
  65. *
  66. * - Contexts can be nested.
  67. * - When entering a context, call
  68. * <code>LoggerNDC::push()</code>
  69. * As a side effect, if there is no nested diagnostic context for the
  70. * current thread, this method will create it.
  71. * - When leaving a context, call
  72. * <code>LoggerNDC::pop()</code>
  73. * - <b>When exiting a thread make sure to call {@link remove()}</b>
  74. *
  75. * <p>There is no penalty for forgetting to match each
  76. * <code>push</code> operation with a corresponding <code>pop</code>,
  77. * except the obvious mismatch between the real application context
  78. * and the context set in the NDC.</p>
  79. *
  80. * <p>If configured to do so, {@link LoggerPatternLayout} and {@link LoggerLayoutTTCC}
  81. * instances automatically retrieve the nested diagnostic
  82. * context for the current thread without any user intervention.
  83. * Hence, even if a servlet is serving multiple clients
  84. * simultaneously, the logs emanating from the same code (belonging to
  85. * the same category) can still be distinguished because each client
  86. * request will have a different NDC tag.</p>
  87. *
  88. *
  89. * @author Marco Vassura
  90. * @version $Revision: 635069 $
  91. * @package log4php
  92. * @since 0.3
  93. */
  94. class LoggerNDC {
  95. /**
  96. * Clear any nested diagnostic information if any. This method is
  97. * useful in cases where the same thread can be potentially used
  98. * over and over in different unrelated contexts.
  99. *
  100. * <p>This method is equivalent to calling the {@link setMaxDepth()}
  101. * method with a zero <var>maxDepth</var> argument.
  102. *
  103. * @static
  104. */
  105. public static function clear()
  106. {
  107. LoggerLog::debug("LoggerNDC::clear()");
  108. $GLOBALS['log4php.LoggerNDC.ht'] = array();
  109. }
  110. /**
  111. * Never use this method directly, use the {@link LoggerLoggingEvent::getNDC()} method instead.
  112. * @static
  113. * @return array
  114. */
  115. public static function get()
  116. {
  117. LoggerLog::debug("LoggerNDC::get()");
  118. return $GLOBALS['log4php.LoggerNDC.ht'];
  119. }
  120. /**
  121. * Get the current nesting depth of this diagnostic context.
  122. *
  123. * @see setMaxDepth()
  124. * @return integer
  125. * @static
  126. */
  127. public static function getDepth()
  128. {
  129. LoggerLog::debug("LoggerNDC::getDepth()");
  130. return sizeof($GLOBALS['log4php.LoggerNDC.ht']);
  131. }
  132. /**
  133. * Clients should call this method before leaving a diagnostic
  134. * context.
  135. *
  136. * <p>The returned value is the value that was pushed last. If no
  137. * context is available, then the empty string "" is returned.</p>
  138. *
  139. * @return string The innermost diagnostic context.
  140. * @static
  141. */
  142. public static function pop()
  143. {
  144. LoggerLog::debug("LoggerNDC::pop()");
  145. if (sizeof($GLOBALS['log4php.LoggerNDC.ht']) > 0) {
  146. return array_pop($GLOBALS['log4php.LoggerNDC.ht']);
  147. } else {
  148. return '';
  149. }
  150. }
  151. /**
  152. * Looks at the last diagnostic context at the top of this NDC
  153. * without removing it.
  154. *
  155. * <p>The returned value is the value that was pushed last. If no
  156. * context is available, then the empty string "" is returned.</p>
  157. * @return string The innermost diagnostic context.
  158. * @static
  159. */
  160. public static function peek()
  161. {
  162. LoggerLog::debug("LoggerNDC::peek()");
  163. if (sizeof($GLOBALS['log4php.LoggerNDC.ht']) > 0) {
  164. return end($GLOBALS['log4php.LoggerNDC.ht']);
  165. } else {
  166. return '';
  167. }
  168. }
  169. /**
  170. * Push new diagnostic context information for the current thread.
  171. *
  172. * <p>The contents of the <var>message</var> parameter is
  173. * determined solely by the client.
  174. *
  175. * @param string $message The new diagnostic context information.
  176. * @static
  177. */
  178. public static function push($message)
  179. {
  180. LoggerLog::debug("LoggerNDC::push()");
  181. array_push($GLOBALS['log4php.LoggerNDC.ht'], (string)$message);
  182. }
  183. /**
  184. * Remove the diagnostic context for this thread.
  185. * @static
  186. */
  187. public static function remove()
  188. {
  189. LoggerLog::debug("LoggerNDC::remove()");
  190. LoggerNDC::clear();
  191. }
  192. /**
  193. * Set maximum depth of this diagnostic context. If the current
  194. * depth is smaller or equal to <var>maxDepth</var>, then no
  195. * action is taken.
  196. *
  197. * <p>This method is a convenient alternative to multiple
  198. * {@link pop()} calls. Moreover, it is often the case that at
  199. * the end of complex call sequences, the depth of the NDC is
  200. * unpredictable. The {@link setMaxDepth()} method circumvents
  201. * this problem.
  202. *
  203. * @param integer $maxDepth
  204. * @see getDepth()
  205. * @static
  206. */
  207. public static function setMaxDepth($maxDepth)
  208. {
  209. LoggerLog::debug("LoggerNDC::setMaxDepth() maxDepth='$maxDepth'");
  210. $maxDepth = (int)$maxDepth;
  211. if ($maxDepth <= LOGGER_NDC_HT_SIZE) {
  212. if (LoggerNDC::getDepth() > $maxDepth) {
  213. $GLOBALS['log4php.LoggerNDC.ht'] = array_slice($GLOBALS['log4php.LoggerNDC.ht'], $maxDepth);
  214. }
  215. $GLOBALS['log4php.LoggerNDC.maxDepth'] = $maxDepth;
  216. }
  217. }
  218. }