LoggerPatternLayout.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage layouts
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. /**
  24. */
  25. require_once(LOG4PHP_DIR . '/helpers/LoggerPatternParser.php');
  26. require_once(LOG4PHP_DIR . '/LoggerLayout.php');
  27. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  28. /**
  29. * Default conversion Pattern
  30. */
  31. define('LOG4PHP_LOGGER_PATTERN_LAYOUT_DEFAULT_CONVERSION_PATTERN', '%m%n');
  32. define('LOG4PHP_LOGGER_PATTERN_LAYOUT_TTCC_CONVERSION_PATTERN', '%r [%t] %p %c %x - %m%n');
  33. /**
  34. * A flexible layout configurable with pattern string.
  35. *
  36. * <p>The goal of this class is to {@link format()} a {@link LoggerLoggingEvent} and return the results as a string.
  37. * The results depend on the conversion pattern.
  38. * The conversion pattern is closely related to the conversion pattern of the printf function in C.
  39. * A conversion pattern is composed of literal text and format control expressions called conversion specifiers.
  40. * You are free to insert any literal text within the conversion pattern.</p>
  41. *
  42. * <p>Each conversion specifier starts with a percent sign (%) and is followed by optional
  43. * format modifiers and a conversion character.</p>
  44. *
  45. * <p>The conversion character specifies the type of data, e.g. category, priority, date, thread name.
  46. * The format modifiers control such things as field width, padding, left and right justification.</p>
  47. *
  48. * The following is a simple example.
  49. *
  50. * <p>Let the conversion pattern be "%-5p [%t]: %m%n" and assume that the log4php environment
  51. * was set to use a LoggerPatternLayout.</p>
  52. *
  53. * Then the statements
  54. * <code>
  55. * $root =& LoggerManager::getRoot();
  56. * $root->debug("Message 1");
  57. * $root->warn("Message 2");
  58. * </code>
  59. * would yield the output
  60. * <pre>
  61. * DEBUG [main]: Message 1
  62. * WARN [main]: Message 2
  63. * </pre>
  64. *
  65. * <p>Note that there is no explicit separator between text and conversion specifiers.</p>
  66. *
  67. * <p>The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character.
  68. * In the example above the conversion specifier %-5p means the priority of the logging event should be
  69. * left justified to a width of five characters.</p>
  70. *
  71. * Not all log4j conversion characters are implemented. The recognized conversion characters are:
  72. * - <b>c</b> Used to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.
  73. * If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed.
  74. * By default the category name is printed in full.
  75. * For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
  76. * - <b>C</b> Used to output the fully qualified class name of the caller issuing the logging request.
  77. * This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.
  78. * If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed.
  79. * By default the class name is output in fully qualified form.
  80. * For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".
  81. * - <b>d</b> Used to output the date of the logging event.
  82. * The date conversion specifier may be followed by a date format specifier enclosed between braces.
  83. * The format specifier follows the {@link PHP_MANUAL#date} function.
  84. * Note that the special character <b>u</b> is used to as microseconds replacement (to avoid replacement,
  85. * use <b>\u</b>).
  86. * For example, %d{H:i:s,u} or %d{d M Y H:i:s,u}. If no date format specifier is given then ISO8601 format is assumed.
  87. * The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat.
  88. * It is recommended to use the predefined log4php date formatters.
  89. * These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying
  90. * AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat.
  91. * For example, %d{ISO8601} or %d{ABSOLUTE}.
  92. * - <b>F</b> Used to output the file name where the logging request was issued.
  93. * - <b>l</b> Used to output location information of the caller which generated the logging event.
  94. * - <b>L</b> Used to output the line number from where the logging request was issued.
  95. * - <b>m</b> Used to output the application supplied message associated with the logging event.
  96. * - <b>M</b> Used to output the method name where the logging request was issued.
  97. * - <b>p</b> Used to output the priority of the logging event.
  98. * - <b>r</b> Used to output the number of milliseconds elapsed since the start of
  99. * the application until the creation of the logging event.
  100. * - <b>t</b> Used to output the name of the thread that generated the logging event.
  101. * - <b>x</b> Used to output the NDC (nested diagnostic context) associated with
  102. * the thread that generated the logging event.
  103. * - <b>X</b> Used to output the MDC (mapped diagnostic context) associated with
  104. * the thread that generated the logging event.
  105. * The X conversion character must be followed by the key for the map placed between braces,
  106. * as in <i>%X{clientNumber}</i> where clientNumber is the key.
  107. * The value in the MDC corresponding to the key will be output.
  108. * See {@link LoggerMDC} class for more details.
  109. * - <b>%</b> The sequence %% outputs a single percent sign.
  110. *
  111. * <p>By default the relevant information is output as is.
  112. * However, with the aid of format modifiers it is possible to change the minimum field width,
  113. * the maximum field width and justification.</p>
  114. *
  115. * <p>The optional format modifier is placed between the percent sign and the conversion character.</p>
  116. * <p>The first optional format modifier is the left justification flag which is just the minus (-) character.
  117. * Then comes the optional minimum field width modifier.
  118. * This is a decimal constant that represents the minimum number of characters to output.
  119. * If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data.
  120. * The value is never truncated.</p>
  121. *
  122. * <p>This behavior can be changed using the maximum field width modifier which is designated by a period
  123. * followed by a decimal constant.
  124. * If the data item is longer than the maximum field,
  125. * then the extra characters are removed from the beginning of the data item and not from the end.
  126. * For example, it the maximum field width is eight and the data item is ten characters long,
  127. * then the first two characters of the data item are dropped.
  128. * This behavior deviates from the printf function in C where truncation is done from the end.</p>
  129. *
  130. * <p>Below are various format modifier examples for the category conversion specifier.</p>
  131. * <pre>
  132. * Format modifier left justify minimum width maximum width comment
  133. * %20c false 20 none Left pad with spaces if the category name
  134. * is less than 20 characters long.
  135. * %-20c true 20 none Right pad with spaces if the category name
  136. * is less than 20 characters long.
  137. * %.30c NA none 30 Truncate from the beginning if the category name
  138. * is longer than 30 characters.
  139. * %20.30c false 20 30 Left pad with spaces if the category name
  140. * is shorter than 20 characters.
  141. * However, if category name is longer than 30 chars,
  142. * then truncate from the beginning.
  143. * %-20.30c true 20 30 Right pad with spaces if the category name is
  144. * shorter than 20 chars.
  145. * However, if category name is longer than 30 chars,
  146. * then truncate from the beginning.
  147. * </pre>
  148. *
  149. * @author VxR <vxr@vxr.it>
  150. * @version $Revision: 1.1 $
  151. * @package log4php
  152. * @subpackage layouts
  153. * @since 0.3
  154. */
  155. class LoggerPatternLayout extends LoggerLayout {
  156. /**
  157. * @var string output buffer appended to when format() is invoked
  158. */
  159. var $sbuf;
  160. /**
  161. * @var string
  162. */
  163. var $pattern;
  164. /**
  165. * @var LoggerPatternConverter head chain
  166. */
  167. var $head;
  168. var $timezone;
  169. /**
  170. * Constructs a PatternLayout using the
  171. * {@link LOG4PHP_LOGGER_PATTERN_LAYOUT_DEFAULT_LAYOUT_PATTERN}.
  172. * The default pattern just produces the application supplied message.
  173. */
  174. function LoggerPatternLayout($pattern = null)
  175. {
  176. if ($pattern === null) {
  177. $this->LoggerPatternLayout(LOG4PHP_LOGGER_PATTERN_LAYOUT_DEFAULT_CONVERSION_PATTERN);
  178. } else {
  179. $this->pattern = $pattern;
  180. }
  181. }
  182. /**
  183. * Set the <b>ConversionPattern</b> option. This is the string which
  184. * controls formatting and consists of a mix of literal content and
  185. * conversion specifiers.
  186. */
  187. function setConversionPattern($conversionPattern)
  188. {
  189. $this->pattern = $conversionPattern;
  190. $patternParser = $this->createPatternParser($this->pattern);
  191. $this->head = $patternParser->parse();
  192. }
  193. /**
  194. * @return string Returns the value of the <b>ConversionPattern</b> option.
  195. */
  196. function getConversionPattern()
  197. {
  198. return $this->pattern;
  199. }
  200. /**
  201. * Does not do anything as options become effective
  202. */
  203. function activateOptions()
  204. {
  205. // nothing to do.
  206. }
  207. function ignoresThrowable()
  208. {
  209. return true;
  210. }
  211. /**
  212. * Returns LoggerPatternParser used to parse the conversion string. Subclasses
  213. * may override this to return a subclass of PatternParser which recognize
  214. * custom conversion characters.
  215. *
  216. * @param string $pattern
  217. * @return LoggerPatternParser
  218. */
  219. function createPatternParser($pattern)
  220. {
  221. return new LoggerPatternParser($pattern);
  222. }
  223. /**
  224. * Produces a formatted string as specified by the conversion pattern.
  225. *
  226. * @param LoggerLoggingEvent $event
  227. * @return string
  228. */
  229. function format($event)
  230. {
  231. LoggerLog::debug("LoggerPatternLayout::format()");
  232. // Reset working stringbuffer
  233. $this->sbuf = '';
  234. $c = $this->head;
  235. while($c !== null) {
  236. $c->format($this->sbuf, $event);
  237. $c = $c->next;
  238. }
  239. return $this->sbuf;
  240. }
  241. }
  242. ?>