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

/scalr-2/tags/scalr-2.0.0/app/src/Lib/IO/Logging/log4php/src/main/php/layouts/LoggerLayoutTTCC.php

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