PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/log4php/Layout/TtccLayout.php

https://github.com/ehutson/log4php
PHP | 217 lines | 90 code | 28 blank | 99 comment | 7 complexity | 1ebe7d412f2554b0faa71c7478812187 MD5 | raw file
  1. <?php
  2. namespace log4php\Layout;
  3. use log4php\LoggerLayout,
  4. \log4php\LoggerLoggingEvent;
  5. /**
  6. * Licensed to the Apache Software Foundation (ASF) under one or more
  7. * contributor license agreements. See the NOTICE file distributed with
  8. * this work for additional information regarding copyright ownership.
  9. * The ASF licenses this file to You under the Apache License, Version 2.0
  10. * (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * @package log4php
  22. */
  23. /**
  24. * TTCC layout format consists of <b>t</b>ime, <b>t</b>hread, <b>c</b>ategory and nested
  25. * diagnostic <b>c</b>ontext information, hence the name.
  26. *
  27. * <p>Each of the four fields can be individually enabled or
  28. * disabled. The time format depends on the <b>DateFormat</b> used.</p>
  29. *
  30. * <p>If no dateFormat is specified it defaults to '%c'.
  31. * See php {@link PHP_MANUAL#date} function for details.</p>
  32. *
  33. * Configurable parameters for this layout are:
  34. * - {@link $threadPrinting} (true|false) enable/disable pid reporting.
  35. * - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting.
  36. * - {@link $contextPrinting} (true|false) enable/disable NDC reporting.
  37. * - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp.
  38. * - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details.
  39. *
  40. * An example how to use this layout:
  41. *
  42. * {@example ../../examples/php/layout_ttcc.php 19}<br>
  43. *
  44. * {@example ../../examples/resources/layout_ttcc.properties 18}<br>
  45. *
  46. * The above would print:<br>
  47. * <samp>02:28 [13714] INFO root - Hello World!</samp>
  48. *
  49. * @version $Revision: 1213283 $
  50. * @package log4php
  51. * @subpackage layouts
  52. */
  53. class TtccLayout extends LoggerLayout
  54. {
  55. // Internal representation of options
  56. protected $threadPrinting = true;
  57. protected $categoryPrefixing = true;
  58. protected $contextPrinting = true;
  59. protected $microSecondsPrinting = true;
  60. /**
  61. * @var string date format. See {@link PHP_MANUAL#strftime} for details
  62. */
  63. protected $dateFormat = '%c';
  64. /**
  65. * Constructor
  66. *
  67. * @param string date format
  68. * @see dateFormat
  69. */
  70. public function __construct($dateFormat = '')
  71. {
  72. if (!empty($dateFormat)) {
  73. $this->dateFormat = $dateFormat;
  74. }
  75. return;
  76. }
  77. /**
  78. * The <b>ThreadPrinting</b> option specifies whether the name of the
  79. * current thread is part of log output or not. This is true by default.
  80. */
  81. public function setThreadPrinting($threadPrinting)
  82. {
  83. $this->setBoolean('threadPrinting', $threadPrinting);
  84. }
  85. /**
  86. * @return boolean Returns value of the <b>ThreadPrinting</b> option.
  87. */
  88. public function getThreadPrinting()
  89. {
  90. return $this->threadPrinting;
  91. }
  92. /**
  93. * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
  94. * name is part of log output or not. This is true by default.
  95. */
  96. public function setCategoryPrefixing($categoryPrefixing)
  97. {
  98. $this->setBoolean('categoryPrefixing', $categoryPrefixing);
  99. }
  100. /**
  101. * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
  102. */
  103. public function getCategoryPrefixing()
  104. {
  105. return $this->categoryPrefixing;
  106. }
  107. /**
  108. * The <b>ContextPrinting</b> option specifies log output will include
  109. * the nested context information belonging to the current thread.
  110. * This is true by default.
  111. */
  112. public function setContextPrinting($contextPrinting)
  113. {
  114. $this->setBoolean('contextPrinting', $contextPrinting);
  115. }
  116. /**
  117. * @return boolean Returns value of the <b>ContextPrinting</b> option.
  118. */
  119. public function getContextPrinting()
  120. {
  121. return $this->contextPrinting;
  122. }
  123. /**
  124. * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
  125. * should be printed at the end of timestamp.
  126. * This is true by default.
  127. */
  128. public function setMicroSecondsPrinting($microSecondsPrinting)
  129. {
  130. $this->setBoolean('microSecondsPrinting', $microSecondsPrinting);
  131. }
  132. /**
  133. * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
  134. */
  135. public function getMicroSecondsPrinting()
  136. {
  137. return $this->microSecondsPrinting;
  138. }
  139. public function setDateFormat($dateFormat)
  140. {
  141. $this->setString('dateFormat', $dateFormat);
  142. }
  143. /**
  144. * @return string
  145. */
  146. public function getDateFormat()
  147. {
  148. return $this->dateFormat;
  149. }
  150. /**
  151. * In addition to the level of the statement and message, the
  152. * returned string includes time, thread, category.
  153. * <p>Time, thread, category are printed depending on options.
  154. *
  155. * @param LoggerLoggingEvent $event
  156. * @return string
  157. */
  158. public function format(LoggerLoggingEvent $event)
  159. {
  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 . ' ';
  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. {
  187. return true;
  188. }
  189. }