PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/php/log4php/layouts/LoggerLayoutTTCC.php

https://code.google.com/p/plexnmt/
PHP | 201 lines | 77 code | 25 blank | 99 comment | 9 complexity | c9b84a435094f3e104ec49c7d9dba57e 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: 1136787 $
  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->threadPrinting = is_bool($threadPrinting) ?
  78. $threadPrinting :
  79. (bool)(strtolower($threadPrinting) == 'true');
  80. }
  81. /**
  82. * @return boolean Returns value of the <b>ThreadPrinting</b> option.
  83. */
  84. public function getThreadPrinting() {
  85. return $this->threadPrinting;
  86. }
  87. /**
  88. * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
  89. * name is part of log output or not. This is true by default.
  90. */
  91. public function setCategoryPrefixing($categoryPrefixing) {
  92. $this->categoryPrefixing = LoggerOptionConverter::toBoolean($categoryPrefixing);
  93. }
  94. /**
  95. * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
  96. */
  97. public function getCategoryPrefixing() {
  98. return $this->categoryPrefixing;
  99. }
  100. /**
  101. * The <b>ContextPrinting</b> option specifies log output will include
  102. * the nested context information belonging to the current thread.
  103. * This is true by default.
  104. */
  105. public function setContextPrinting($contextPrinting) {
  106. $this->contextPrinting = LoggerOptionConverter::toBoolean($contextPrinting);
  107. }
  108. /**
  109. * @return boolean Returns value of the <b>ContextPrinting</b> option.
  110. */
  111. public function getContextPrinting() {
  112. return $this->contextPrinting;
  113. }
  114. /**
  115. * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
  116. * should be printed at the end of timestamp.
  117. * This is true by default.
  118. */
  119. public function setMicroSecondsPrinting($microSecondsPrinting) {
  120. $this->microSecondsPrinting = is_bool($microSecondsPrinting) ?
  121. $microSecondsPrinting :
  122. (bool)(strtolower($microSecondsPrinting) == 'true');
  123. }
  124. /**
  125. * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
  126. */
  127. public function getMicroSecondsPrinting() {
  128. return $this->microSecondsPrinting;
  129. }
  130. public function setDateFormat($dateFormat) {
  131. $this->dateFormat = $dateFormat;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function getDateFormat() {
  137. return $this->dateFormat;
  138. }
  139. /**
  140. * In addition to the level of the statement and message, the
  141. * returned string includes time, thread, category.
  142. * <p>Time, thread, category are printed depending on options.
  143. *
  144. * @param LoggerLoggingEvent $event
  145. * @return string
  146. */
  147. public function format(LoggerLoggingEvent $event) {
  148. $timeStamp = (float)$event->getTimeStamp();
  149. $format = strftime($this->dateFormat, (int)$timeStamp);
  150. if ($this->microSecondsPrinting) {
  151. $usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
  152. $format .= sprintf(',%03d', $usecs);
  153. }
  154. $format .= ' ';
  155. if ($this->threadPrinting) {
  156. $format .= '['.getmypid().'] ';
  157. }
  158. $level = $event->getLevel();
  159. $format .= $level.' ';
  160. if($this->categoryPrefixing) {
  161. $format .= $event->getLoggerName().' ';
  162. }
  163. if($this->contextPrinting) {
  164. $ndc = $event->getNDC();
  165. if($ndc != null) {
  166. $format .= $ndc.' ';
  167. }
  168. }
  169. $format .= '- '.$event->getRenderedMessage();
  170. $format .= PHP_EOL;
  171. return $format;
  172. }
  173. public function ignoresThrowable() {
  174. return true;
  175. }
  176. }