OSDN Git Service

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