PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/src/externals/apache-log4php-2.0.0-incubating/src/main/php/layouts/LoggerLayoutTTCC.php

http://scalr.googlecode.com/
PHP | 215 lines | 79 code | 27 blank | 109 comment | 9 complexity | 31e5b4a3d9dcd6ab324db43ee11ad7d1 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  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. * TTCC layout format consists of <b>t</b>ime, <b>t</b>hread, <b>c</b>ategory and nested
  22. * diagnostic <b>c</b>ontext information, hence the name.
  23. *
  24. * <p>Each of the four fields can be individually enabled or
  25. * disabled. The time format depends on the <b>DateFormat</b> used.</p>
  26. *
  27. * <p>If no dateFormat is specified it defaults to '%c'.
  28. * See php {@link PHP_MANUAL#date} function for details.</p>
  29. *
  30. * Configurable parameters for this layout are:
  31. * - {@link $threadPrinting} (true|false) enable/disable pid reporting.
  32. * - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting.
  33. * - {@link $contextPrinting} (true|false) enable/disable NDC reporting.
  34. * - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp.
  35. * - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details.
  36. *
  37. * An example how to use this layout:
  38. *
  39. * {@example ../../examples/php/layout_ttcc.php 19}<br>
  40. *
  41. * {@example ../../examples/resources/layout_ttcc.properties 18}<br>
  42. *
  43. * The above would print:<br>
  44. * <samp>02:28 [13714] INFO root - Hello World!</samp>
  45. *
  46. * @version $Revision: 883108 $
  47. * @package log4php
  48. * @subpackage layouts
  49. */
  50. class LoggerLayoutTTCC extends LoggerLayout {
  51. /**
  52. * String constant designating no time information. Current value of
  53. * this constant is <b>NULL</b>.
  54. */
  55. // TODO: not used?
  56. const LOG4PHP_LOGGER_LAYOUT_NULL_DATE_FORMAT = 'NULL';
  57. /**
  58. * String constant designating relative time. Current value of
  59. * this constant is <b>RELATIVE</b>.
  60. */
  61. // TODO: not used?
  62. const LOG4PHP_LOGGER_LAYOUT_RELATIVE_TIME_DATE_FORMAT = 'RELATIVE';
  63. // Internal representation of options
  64. protected $threadPrinting = true;
  65. protected $categoryPrefixing = true;
  66. protected $contextPrinting = true;
  67. protected $microSecondsPrinting = true;
  68. /**
  69. * @var string date format. See {@link PHP_MANUAL#strftime} for details
  70. */
  71. protected $dateFormat = '%c';
  72. /**
  73. * Constructor
  74. *
  75. * @param string date format
  76. * @see dateFormat
  77. */
  78. public function __construct($dateFormat = '') {
  79. if (!empty($dateFormat)) {
  80. $this->dateFormat = $dateFormat;
  81. }
  82. return;
  83. }
  84. /**
  85. * The <b>ThreadPrinting</b> option specifies whether the name of the
  86. * current thread is part of log output or not. This is true by default.
  87. */
  88. public function setThreadPrinting($threadPrinting) {
  89. $this->threadPrinting = is_bool($threadPrinting) ?
  90. $threadPrinting :
  91. (bool)(strtolower($threadPrinting) == 'true');
  92. }
  93. /**
  94. * @return boolean Returns value of the <b>ThreadPrinting</b> option.
  95. */
  96. public function getThreadPrinting() {
  97. return $this->threadPrinting;
  98. }
  99. /**
  100. * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
  101. * name is part of log output or not. This is true by default.
  102. */
  103. public function setCategoryPrefixing($categoryPrefixing) {
  104. $this->categoryPrefixing = LoggerOptionConverter::toBoolean($categoryPrefixing);
  105. }
  106. /**
  107. * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
  108. */
  109. public function getCategoryPrefixing() {
  110. return $this->categoryPrefixing;
  111. }
  112. /**
  113. * The <b>ContextPrinting</b> option specifies log output will include
  114. * the nested context information belonging to the current thread.
  115. * This is true by default.
  116. */
  117. public function setContextPrinting($contextPrinting) {
  118. $this->contextPrinting = LoggerOptionConverter::toBoolean($contextPrinting);
  119. }
  120. /**
  121. * @return boolean Returns value of the <b>ContextPrinting</b> option.
  122. */
  123. public function getContextPrinting() {
  124. return $this->contextPrinting;
  125. }
  126. /**
  127. * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
  128. * should be printed at the end of timestamp.
  129. * This is true by default.
  130. */
  131. public function setMicroSecondsPrinting($microSecondsPrinting) {
  132. $this->microSecondsPrinting = is_bool($microSecondsPrinting) ?
  133. $microSecondsPrinting :
  134. (bool)(strtolower($microSecondsPrinting) == 'true');
  135. }
  136. /**
  137. * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
  138. */
  139. public function getMicroSecondsPrinting() {
  140. return $this->microSecondsPrinting;
  141. }
  142. public function setDateFormat($dateFormat) {
  143. $this->dateFormat = $dateFormat;
  144. }
  145. /**
  146. * @return string
  147. */
  148. public function getDateFormat() {
  149. return $this->dateFormat;
  150. }
  151. /**
  152. * In addition to the level of the statement and message, the
  153. * returned string includes time, thread, category.
  154. * <p>Time, thread, category are printed depending on options.
  155. *
  156. * @param LoggerLoggingEvent $event
  157. * @return string
  158. */
  159. public function format(LoggerLoggingEvent $event) {
  160. $timeStamp = (float)$event->getTimeStamp();
  161. $format = strftime($this->dateFormat, (int)$timeStamp);
  162. if ($this->microSecondsPrinting) {
  163. $usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
  164. $format .= sprintf(',%03d', $usecs);
  165. }
  166. $format .= ' ';
  167. if ($this->threadPrinting) {
  168. $format .= '['.getmypid().'] ';
  169. }
  170. $level = $event->getLevel();
  171. $format .= $level->toString().' ';
  172. if($this->categoryPrefixing) {
  173. $format .= $event->getLoggerName().' ';
  174. }
  175. if($this->contextPrinting) {
  176. $ndc = $event->getNDC();
  177. if($ndc != null) {
  178. $format .= $ndc.' ';
  179. }
  180. }
  181. $format .= '- '.$event->getRenderedMessage();
  182. $format .= PHP_EOL;
  183. return $format;
  184. }
  185. public function ignoresThrowable() {
  186. return true;
  187. }
  188. }