PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/apache-log4php/trunk/src/main/php/layouts/LoggerLayoutTTCC.php

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