source: tmp/version-2_5-test/data/module/log4php/php4/log4php/layouts/LoggerPatternLayout.php @ 18609

Revision 18609, 12.5 KB checked in by kajiwara, 14 years ago (diff)

正式版にナイトリービルド版をマージしてみるテスト

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