PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/log4php/layouts/LoggerLayoutTTCC.php

http://sabre-zarafa.googlecode.com/
PHP | 197 lines | 73 code | 25 blank | 99 comment | 7 complexity | 3f2175a87f268e4617c52e4e392b8225 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. * 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: 31 $
  47. * @package log4php
  48. * @subpackage layouts
  49. */
  50. class LoggerLayoutTTCC extends LoggerLayout {
  51. // Internal representation of options
  52. protected $threadPrinting = true;
  53. protected $categoryPrefixing = true;
  54. protected $contextPrinting = true;
  55. protected $microSecondsPrinting = true;
  56. /**
  57. * @var string date format. See {@link PHP_MANUAL#strftime} for details
  58. */
  59. protected $dateFormat = '%c';
  60. /**
  61. * Constructor
  62. *
  63. * @param string date format
  64. * @see dateFormat
  65. */
  66. public function __construct($dateFormat = '') {
  67. if (!empty($dateFormat)) {
  68. $this->dateFormat = $dateFormat;
  69. }
  70. return;
  71. }
  72. /**
  73. * The <b>ThreadPrinting</b> option specifies whether the name of the
  74. * current thread is part of log output or not. This is true by default.
  75. */
  76. public function setThreadPrinting($threadPrinting) {
  77. $this->setBoolean('threadPrinting', $threadPrinting);
  78. }
  79. /**
  80. * @return boolean Returns value of the <b>ThreadPrinting</b> option.
  81. */
  82. public function getThreadPrinting() {
  83. return $this->threadPrinting;
  84. }
  85. /**
  86. * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
  87. * name is part of log output or not. This is true by default.
  88. */
  89. public function setCategoryPrefixing($categoryPrefixing) {
  90. $this->setBoolean('categoryPrefixing', $categoryPrefixing);
  91. }
  92. /**
  93. * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
  94. */
  95. public function getCategoryPrefixing() {
  96. return $this->categoryPrefixing;
  97. }
  98. /**
  99. * The <b>ContextPrinting</b> option specifies log output will include
  100. * the nested context information belonging to the current thread.
  101. * This is true by default.
  102. */
  103. public function setContextPrinting($contextPrinting) {
  104. $this->setBoolean('contextPrinting', $contextPrinting);
  105. }
  106. /**
  107. * @return boolean Returns value of the <b>ContextPrinting</b> option.
  108. */
  109. public function getContextPrinting() {
  110. return $this->contextPrinting;
  111. }
  112. /**
  113. * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
  114. * should be printed at the end of timestamp.
  115. * This is true by default.
  116. */
  117. public function setMicroSecondsPrinting($microSecondsPrinting) {
  118. $this->setBoolean('microSecondsPrinting', $microSecondsPrinting);
  119. }
  120. /**
  121. * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
  122. */
  123. public function getMicroSecondsPrinting() {
  124. return $this->microSecondsPrinting;
  125. }
  126. public function setDateFormat($dateFormat) {
  127. $this->setString('dateFormat', $dateFormat);
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function getDateFormat() {
  133. return $this->dateFormat;
  134. }
  135. /**
  136. * In addition to the level of the statement and message, the
  137. * returned string includes time, thread, category.
  138. * <p>Time, thread, category are printed depending on options.
  139. *
  140. * @param LoggerLoggingEvent $event
  141. * @return string
  142. */
  143. public function format(LoggerLoggingEvent $event) {
  144. $timeStamp = (float)$event->getTimeStamp();
  145. $format = strftime($this->dateFormat, (int)$timeStamp);
  146. if ($this->microSecondsPrinting) {
  147. $usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
  148. $format .= sprintf(',%03d', $usecs);
  149. }
  150. $format .= ' ';
  151. if ($this->threadPrinting) {
  152. $format .= '['.getmypid().'] ';
  153. }
  154. $level = $event->getLevel();
  155. $format .= $level.' ';
  156. if($this->categoryPrefixing) {
  157. $format .= $event->getLoggerName().' ';
  158. }
  159. if($this->contextPrinting) {
  160. $ndc = $event->getNDC();
  161. if($ndc != null) {
  162. $format .= $ndc.' ';
  163. }
  164. }
  165. $format .= '- '.$event->getRenderedMessage();
  166. $format .= PHP_EOL;
  167. return $format;
  168. }
  169. public function ignoresThrowable() {
  170. return true;
  171. }
  172. }