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

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

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

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