PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/log4php.debug/layouts/LoggerLayoutTTCC.php

https://bitbucket.org/jhunsinfotech/blue-blues
PHP | 240 lines | 100 code | 31 blank | 109 comment | 16 complexity | 462e46cd8ebb41064a40ace5e969003c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage layouts
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. if (!defined('LOG4PHP_LINE_SEP')) {
  24. if (substr(php_uname(), 0, 7) == "Windows") {
  25. /**
  26. * @ignore
  27. */
  28. define('LOG4PHP_LINE_SEP', "\r\n");
  29. } else {
  30. /**
  31. * @ignore
  32. */
  33. define('LOG4PHP_LINE_SEP', "\n");
  34. }
  35. }
  36. /**
  37. */
  38. require_once(LOG4PHP_DIR . '/LoggerLayout.php');
  39. /**
  40. * String constant designating no time information. Current value of
  41. * this constant is <b>NULL</b>.
  42. */
  43. define ('LOG4PHP_LOGGER_LAYOUT_NULL_DATE_FORMAT', 'NULL');
  44. /**
  45. * String constant designating relative time. Current value of
  46. * this constant is <b>RELATIVE</b>.
  47. */
  48. define ('LOG4PHP_LOGGER_LAYOUT_RELATIVE_TIME_DATE_FORMAT', 'RELATIVE');
  49. /**
  50. * TTCC layout format consists of time, thread, category and nested
  51. * diagnostic context information, hence the name.
  52. *
  53. * <p>Each of the four fields can be individually enabled or
  54. * disabled. The time format depends on the <b>DateFormat</b> used.</p>
  55. *
  56. * <p>If no dateFormat is specified it defaults to '%c'.
  57. * See php {@link PHP_MANUAL#date} function for details.</p>
  58. *
  59. * Params:
  60. * - {@link $threadPrinting} (true|false) enable/disable pid reporting.
  61. * - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting.
  62. * - {@link $contextPrinting} (true|false) enable/disable NDC reporting.
  63. * - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp.
  64. * - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details.
  65. *
  66. * @author VxR <vxr@vxr.it>
  67. * @version $Revision: 1.12 $
  68. * @package log4php
  69. * @subpackage layouts
  70. */
  71. class LoggerLayoutTTCC extends LoggerLayout {
  72. // Internal representation of options
  73. var $threadPrinting = true;
  74. var $categoryPrefixing = true;
  75. var $contextPrinting = true;
  76. var $microSecondsPrinting = true;
  77. /**
  78. * @var string date format. See {@link PHP_MANUAL#strftime} for details
  79. */
  80. var $dateFormat = '%c';
  81. /**
  82. * Constructor
  83. *
  84. * @param string date format
  85. * @see dateFormat
  86. */
  87. function LoggerLayoutTTCC($dateFormat = '')
  88. {
  89. if (!empty($dateFormat))
  90. $this->dateFormat = $dateFormat;
  91. return;
  92. }
  93. /**
  94. * The <b>ThreadPrinting</b> option specifies whether the name of the
  95. * current thread is part of log output or not. This is true by default.
  96. */
  97. function setThreadPrinting($threadPrinting)
  98. {
  99. $this->threadPrinting = is_bool($threadPrinting) ?
  100. $threadPrinting :
  101. (bool)(strtolower($threadPrinting) == 'true');
  102. }
  103. /**
  104. * @return boolean Returns value of the <b>ThreadPrinting</b> option.
  105. */
  106. function getThreadPrinting() {
  107. return $this->threadPrinting;
  108. }
  109. /**
  110. * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
  111. * name is part of log output or not. This is true by default.
  112. */
  113. function setCategoryPrefixing($categoryPrefixing)
  114. {
  115. $this->categoryPrefixing = is_bool($categoryPrefixing) ?
  116. $categoryPrefixing :
  117. (bool)(strtolower($categoryPrefixing) == 'true');
  118. }
  119. /**
  120. * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
  121. */
  122. function getCategoryPrefixing() {
  123. return $this->categoryPrefixing;
  124. }
  125. /**
  126. * The <b>ContextPrinting</b> option specifies log output will include
  127. * the nested context information belonging to the current thread.
  128. * This is true by default.
  129. */
  130. function setContextPrinting($contextPrinting) {
  131. $this->contextPrinting = is_bool($contextPrinting) ?
  132. $contextPrinting :
  133. (bool)(strtolower($contextPrinting) == 'true');
  134. }
  135. /**
  136. * @return boolean Returns value of the <b>ContextPrinting</b> option.
  137. */
  138. function getContextPrinting()
  139. {
  140. return $this->contextPrinting;
  141. }
  142. /**
  143. * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
  144. * should be printed at the end of timestamp.
  145. * This is true by default.
  146. */
  147. function setMicroSecondsPrinting($microSecondsPrinting) {
  148. $this->microSecondsPrinting = is_bool($microSecondsPrinting) ?
  149. $microSecondsPrinting :
  150. (bool)(strtolower($microSecondsPrinting) == 'true');
  151. }
  152. /**
  153. * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
  154. */
  155. function getMicroSecondsPrinting()
  156. {
  157. return $this->microSecondsPrinting;
  158. }
  159. function setDateFormat($dateFormat)
  160. {
  161. $this->dateFormat = $dateFormat;
  162. }
  163. /**
  164. * @return string
  165. */
  166. function getDateFormat()
  167. {
  168. return $this->dateFormat;
  169. }
  170. /**
  171. * In addition to the level of the statement and message, the
  172. * returned string includes time, thread, category.
  173. * <p>Time, thread, category are printed depending on options.
  174. *
  175. * @param LoggerLoggingEvent $event
  176. * @return string
  177. */
  178. function format($event)
  179. {
  180. $timeStamp = (float)$event->getTimeStamp();
  181. $format = strftime($this->dateFormat, (int)$timeStamp);
  182. if ($this->microSecondsPrinting) {
  183. $usecs = round(($timeStamp - (int)$timeStamp) * 1000);
  184. $format .= sprintf(',%03d', $usecs);
  185. }
  186. $format .= ' ';
  187. if ($this->threadPrinting)
  188. $format .= '['.getmypid().'] ';
  189. $level = $event->getLevel();
  190. $format .= $level->toString().' ';
  191. if($this->categoryPrefixing) {
  192. $format .= $event->getLoggerName().' ';
  193. }
  194. if($this->contextPrinting) {
  195. $ndc = $event->getNDC();
  196. if($ndc != null) {
  197. $format .= $ndc.' ';
  198. }
  199. }
  200. $format .= '- '.$event->getRenderedMessage();
  201. $format .= LOG4PHP_LINE_SEP;
  202. return $format;
  203. }
  204. function ignoresThrowable()
  205. {
  206. return true;
  207. }
  208. }
  209. ?>