1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef _LOG4CXX_PATTERN_LAYOUT_H
|
---|
19 | #define _LOG4CXX_PATTERN_LAYOUT_H
|
---|
20 |
|
---|
21 | #if defined(_MSC_VER)
|
---|
22 | #pragma warning ( push )
|
---|
23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
24 | #endif
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | #include <log4cxx/layout.h>
|
---|
29 | #include <log4cxx/pattern/loggingeventpatternconverter.h>
|
---|
30 | #include <log4cxx/pattern/formattinginfo.h>
|
---|
31 | #include <log4cxx/pattern/patternparser.h>
|
---|
32 |
|
---|
33 | namespace log4cxx
|
---|
34 | {
|
---|
35 | LOG4CXX_LIST_DEF(LoggingEventPatternConverterList, log4cxx::pattern::LoggingEventPatternConverterPtr);
|
---|
36 | LOG4CXX_LIST_DEF(FormattingInfoList, log4cxx::pattern::FormattingInfoPtr);
|
---|
37 |
|
---|
38 | /**
|
---|
39 |
|
---|
40 | A flexible layout configurable with pattern string.
|
---|
41 |
|
---|
42 | <p>The goal of this class is to #format a {@link
|
---|
43 | spi::LoggingEvent LoggingEvent} and return the results as a string.
|
---|
44 | The results depend on the <em>conversion pattern</em>.
|
---|
45 |
|
---|
46 | <p>The conversion pattern is closely related to the conversion
|
---|
47 | pattern of the printf function in C. A conversion pattern is
|
---|
48 | composed of literal text and format control expressions called
|
---|
49 | <em>conversion specifiers</em>.
|
---|
50 |
|
---|
51 | <p><i>You are free to insert any literal text within the conversion
|
---|
52 | pattern.</i>
|
---|
53 |
|
---|
54 | <p>Each conversion specifier starts with a percent sign (\%) and is
|
---|
55 | followed by optional <em>format modifiers</em> and a <em>conversion
|
---|
56 | character</em>. The conversion character specifies the type of
|
---|
57 | data, e.g. logger, level, date, thread name. The format
|
---|
58 | modifiers control such things as field width, padding, left and
|
---|
59 | right justification. The following is a simple example.
|
---|
60 |
|
---|
61 | <p>Let the conversion pattern be <b>"\%-5p [\%t]: \%m\%n"</b> and assume
|
---|
62 | that the log4cxx environment was set to use a PatternLayout. Then the
|
---|
63 | statements
|
---|
64 | <pre>
|
---|
65 | LoggerPtr root = Logger::getRoot();
|
---|
66 | root->debug("Message 1");
|
---|
67 | root->warn("Message 2");
|
---|
68 | </pre>
|
---|
69 | would yield the output
|
---|
70 | <pre>
|
---|
71 | DEBUG [main]: Message 1
|
---|
72 | WARN [main]: Message 2
|
---|
73 | </pre>
|
---|
74 |
|
---|
75 | <p>Note that there is no explicit separator between text and
|
---|
76 | conversion specifiers. The pattern parser knows when it has reached
|
---|
77 | the end of a conversion specifier when it reads a conversion
|
---|
78 | character. In the example above the conversion specifier
|
---|
79 | <b>\%-5p</b> means the level of the logging event should be left
|
---|
80 | justified to a width of five characters.
|
---|
81 |
|
---|
82 | The recognized conversion characters are
|
---|
83 |
|
---|
84 | <p>
|
---|
85 | <table border="1" CELLPADDING="8">
|
---|
86 | <tr>
|
---|
87 | <td align=center><b>Conversion Character</b></td>
|
---|
88 | <td align=center><b>Effect</b></td>
|
---|
89 | </tr>
|
---|
90 |
|
---|
91 | <tr>
|
---|
92 | <td align=center><b>c</b></td>
|
---|
93 |
|
---|
94 | <td>Used to output the logger of the logging event. The
|
---|
95 | logger conversion specifier can be optionally followed by
|
---|
96 | <em>precision specifier</em>, that is a decimal constant in
|
---|
97 | brackets.
|
---|
98 |
|
---|
99 | <p>If a precision specifier is given, then only the corresponding
|
---|
100 | number of right most components of the logger name will be
|
---|
101 | printed. By default the logger name is printed in full.
|
---|
102 |
|
---|
103 | <p>For example, for the logger name "a.b.c" the pattern
|
---|
104 | <b>\%c{2}</b> will output "b.c".
|
---|
105 |
|
---|
106 | </td>
|
---|
107 | </tr>
|
---|
108 |
|
---|
109 | <tr> <td align=center><b>d</b></td> <td>Used to output the date of
|
---|
110 | the logging event. The date conversion specifier may be
|
---|
111 | followed by a set of braces containing a
|
---|
112 | date and time pattern string compatible with java.text.SimpleDateFormat,
|
---|
113 | <em>ABSOLUTE</em>, <em>DATE</em> or <em>ISO8601</em>.
|
---|
114 | For example, <b>%d{HH:mm:ss,SSS}</b>,
|
---|
115 | <b>%d{dd MMM yyyy HH:mm:ss,SSS}</b> or
|
---|
116 | <b>%d{DATE}</b>. If no date format specifier is given then
|
---|
117 | ISO8601 format is assumed.
|
---|
118 | </td>
|
---|
119 | </tr>
|
---|
120 |
|
---|
121 | <tr>
|
---|
122 | <td align=center><b>F</b></td>
|
---|
123 |
|
---|
124 | <td>Used to output the file name where the logging request was
|
---|
125 | issued.
|
---|
126 |
|
---|
127 | </tr>
|
---|
128 |
|
---|
129 | <tr>
|
---|
130 | <td align=center><b>l</b></td>
|
---|
131 |
|
---|
132 | <td>Used to output location information of the caller which generated
|
---|
133 | the logging event.
|
---|
134 |
|
---|
135 | </td>
|
---|
136 | </tr>
|
---|
137 |
|
---|
138 | <tr>
|
---|
139 | <td align=center><b>L</b></td>
|
---|
140 |
|
---|
141 | <td>Used to output the line number from where the logging request
|
---|
142 | was issued.
|
---|
143 | </tr>
|
---|
144 |
|
---|
145 |
|
---|
146 | <tr>
|
---|
147 | <td align=center><b>m</b></td>
|
---|
148 | <td>Used to output the application supplied message associated with
|
---|
149 | the logging event.</td>
|
---|
150 | </tr>
|
---|
151 |
|
---|
152 | <tr>
|
---|
153 | <td align=center><b>n</b></td>
|
---|
154 |
|
---|
155 | <td>Outputs the platform dependent line separator character or
|
---|
156 | characters.
|
---|
157 |
|
---|
158 | <p>This conversion character offers practically the same
|
---|
159 | performance as using non-portable line separator strings such as
|
---|
160 | "\n", or "\r\n". Thus, it is the preferred way of specifying a
|
---|
161 | line separator.
|
---|
162 |
|
---|
163 |
|
---|
164 | </tr>
|
---|
165 |
|
---|
166 | <tr>
|
---|
167 | <td align=center><b>p</b></td>
|
---|
168 | <td>Used to output the level of the logging event.</td>
|
---|
169 | </tr>
|
---|
170 |
|
---|
171 | <tr>
|
---|
172 |
|
---|
173 | <td align=center><b>r</b></td>
|
---|
174 |
|
---|
175 | <td>Used to output the number of milliseconds elapsed since the start
|
---|
176 | of the application until the creation of the logging event.</td>
|
---|
177 | </tr>
|
---|
178 |
|
---|
179 |
|
---|
180 | <tr>
|
---|
181 | <td align=center><b>t</b></td>
|
---|
182 |
|
---|
183 | <td>Used to output the name of the thread that generated the
|
---|
184 | logging event.</td>
|
---|
185 |
|
---|
186 | </tr>
|
---|
187 |
|
---|
188 | <tr>
|
---|
189 |
|
---|
190 | <td align=center><b>x</b></td>
|
---|
191 |
|
---|
192 | <td>Used to output the NDC (nested diagnostic context) associated
|
---|
193 | with the thread that generated the logging event.
|
---|
194 | </td>
|
---|
195 | </tr>
|
---|
196 |
|
---|
197 |
|
---|
198 | <tr>
|
---|
199 | <td align=center><b>X</b></td>
|
---|
200 |
|
---|
201 | <td>
|
---|
202 |
|
---|
203 | <p>Used to output the MDC (mapped diagnostic context) associated
|
---|
204 | with the thread that generated the logging event. The <b>X</b>
|
---|
205 | conversion character <em>must</em> be followed by the key for the
|
---|
206 | map placed between braces, as in <b>\%X{clientNumber}</b> where
|
---|
207 | <code>clientNumber</code> is the key. The value in the MDC
|
---|
208 | corresponding to the key will be output.</p>
|
---|
209 |
|
---|
210 | <p>See MDC class for more details.
|
---|
211 | </p>
|
---|
212 |
|
---|
213 | </td>
|
---|
214 | </tr>
|
---|
215 |
|
---|
216 | <tr>
|
---|
217 |
|
---|
218 | <td align=center><b>\%</b></td>
|
---|
219 |
|
---|
220 | <td>The sequence \%\% outputs a single percent sign.
|
---|
221 | </td>
|
---|
222 | </tr>
|
---|
223 |
|
---|
224 | </table>
|
---|
225 |
|
---|
226 | <p>By default the relevant information is output as is. However,
|
---|
227 | with the aid of format modifiers it is possible to change the
|
---|
228 | minimum field width, the maximum field width and justification.
|
---|
229 |
|
---|
230 | <p>The optional format modifier is placed between the percent sign
|
---|
231 | and the conversion character.
|
---|
232 |
|
---|
233 | <p>The first optional format modifier is the <em>left justification
|
---|
234 | flag</em> which is just the minus (-) character. Then comes the
|
---|
235 | optional <em>minimum field width</em> modifier. This is a decimal
|
---|
236 | constant that represents the minimum number of characters to
|
---|
237 | output. If the data item requires fewer characters, it is padded on
|
---|
238 | either the left or the right until the minimum width is
|
---|
239 | reached. The default is to pad on the left (right justify) but you
|
---|
240 | can specify right padding with the left justification flag. The
|
---|
241 | padding character is space. If the data item is larger than the
|
---|
242 | minimum field width, the field is expanded to accommodate the
|
---|
243 | data. The value is never truncated.
|
---|
244 |
|
---|
245 | <p>This behavior can be changed using the <em>maximum field
|
---|
246 | width</em> modifier which is designated by a period followed by a
|
---|
247 | decimal constant. If the data item is longer than the maximum
|
---|
248 | field, then the extra characters are removed from the
|
---|
249 | <em>beginning</em> of the data item and not from the end. For
|
---|
250 | example, it the maximum field width is eight and the data item is
|
---|
251 | ten characters long, then the first two characters of the data item
|
---|
252 | are dropped. This behavior deviates from the printf function in C
|
---|
253 | where truncation is done from the end.
|
---|
254 |
|
---|
255 | <p>Below are various format modifier examples for the logger
|
---|
256 | conversion specifier.
|
---|
257 |
|
---|
258 | <p>
|
---|
259 | <TABLE BORDER=1 CELLPADDING=8>
|
---|
260 | <tr>
|
---|
261 | <td align=center><b>Format modifier</b></td>
|
---|
262 | <td align=center><b>left justify</b></td>
|
---|
263 | <td align=center><b>minimum width</b></td>
|
---|
264 | <td align=center><b>maximum width</b></td>
|
---|
265 | <td align=center><b>comment</b></td>
|
---|
266 | </tr>
|
---|
267 |
|
---|
268 | <tr>
|
---|
269 | <td align=center>\%20c</td>
|
---|
270 | <td align=center>false</td>
|
---|
271 | <td align=center>20</td>
|
---|
272 | <td align=center>none</td>
|
---|
273 |
|
---|
274 | <td>Left pad with spaces if the logger name is less than 20
|
---|
275 | characters long.
|
---|
276 |
|
---|
277 | <tr> <td align=center>\%-20c</td> <td align=center>true</td> <td
|
---|
278 | align=center>20</td> <td align=center>none</td> <td>Right pad with
|
---|
279 | spaces if the logger name is less than 20 characters long.
|
---|
280 |
|
---|
281 | <tr>
|
---|
282 | <td align=center>\%.30c</td>
|
---|
283 | <td align=center>NA</td>
|
---|
284 | <td align=center>none</td>
|
---|
285 | <td align=center>30</td>
|
---|
286 |
|
---|
287 | <td>Truncate from the beginning if the logger name is longer than 30
|
---|
288 | characters.
|
---|
289 |
|
---|
290 | <tr>
|
---|
291 | <td align=center>\%20.30c</td>
|
---|
292 | <td align=center>false</td>
|
---|
293 | <td align=center>20</td>
|
---|
294 | <td align=center>30</td>
|
---|
295 |
|
---|
296 | <td>Left pad with spaces if the logger name is shorter than 20
|
---|
297 | characters. However, if logger name is longer than 30 characters,
|
---|
298 | then truncate from the beginning.
|
---|
299 |
|
---|
300 | <tr>
|
---|
301 | <td align=center>\%-20.30c</td>
|
---|
302 | <td align=center>true</td>
|
---|
303 | <td align=center>20</td>
|
---|
304 | <td align=center>30</td>
|
---|
305 |
|
---|
306 | <td>Right pad with spaces if the logger name is shorter than 20
|
---|
307 | characters. However, if logger name is longer than 30 characters,
|
---|
308 | then truncate from the beginning.
|
---|
309 |
|
---|
310 | </table>
|
---|
311 |
|
---|
312 | <p>Below are some examples of conversion patterns.
|
---|
313 |
|
---|
314 | <p><b>\%r [\%t] \%-5p \%c \%x - \%m\n</b>
|
---|
315 | <p>This is essentially the TTCC layout.
|
---|
316 |
|
---|
317 | <p><b>\%-6r [\%15.15t] \%-5p \%30.30c \%x - \%m\n</b>
|
---|
318 |
|
---|
319 | <p>Similar to the TTCC layout except that the relative time is
|
---|
320 | right padded if less than 6 digits, thread name is right padded if
|
---|
321 | less than 15 characters and truncated if longer and the logger
|
---|
322 | name is left padded if shorter than 30 characters and truncated if
|
---|
323 | longer.
|
---|
324 |
|
---|
325 | <p>The above text is largely inspired from Peter A. Darnell and
|
---|
326 | Philip E. Margolis' highly recommended book "C -- a Software
|
---|
327 | Engineering Approach", ISBN 0-387-97389-3.
|
---|
328 | */
|
---|
329 | class LOG4CXX_EXPORT PatternLayout : public Layout
|
---|
330 | {
|
---|
331 | /**
|
---|
332 | * Conversion pattern.
|
---|
333 | */
|
---|
334 | LogString conversionPattern;
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Pattern converters.
|
---|
338 | */
|
---|
339 | LoggingEventPatternConverterList patternConverters;
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Field widths and alignment corresponding to pattern converters.
|
---|
343 | */
|
---|
344 | FormattingInfoList patternFields;
|
---|
345 |
|
---|
346 |
|
---|
347 | public:
|
---|
348 | DECLARE_LOG4CXX_OBJECT(PatternLayout)
|
---|
349 | BEGIN_LOG4CXX_CAST_MAP()
|
---|
350 | LOG4CXX_CAST_ENTRY(PatternLayout)
|
---|
351 | LOG4CXX_CAST_ENTRY_CHAIN(Layout)
|
---|
352 | END_LOG4CXX_CAST_MAP()
|
---|
353 |
|
---|
354 | /**
|
---|
355 | Does nothing
|
---|
356 | */
|
---|
357 | PatternLayout();
|
---|
358 |
|
---|
359 | /**
|
---|
360 | Constructs a PatternLayout using the supplied conversion pattern.
|
---|
361 | */
|
---|
362 | PatternLayout(const LogString& pattern);
|
---|
363 |
|
---|
364 | /**
|
---|
365 | Set the <b>ConversionPattern</b> option. This is the string which
|
---|
366 | controls formatting and consists of a mix of literal content and
|
---|
367 | conversion specifiers.
|
---|
368 | */
|
---|
369 | void setConversionPattern(const LogString& conversionPattern);
|
---|
370 |
|
---|
371 | /**
|
---|
372 | Returns the value of the <b>ConversionPattern</b> option.
|
---|
373 | */
|
---|
374 | inline LogString getConversionPattern() const
|
---|
375 | { return conversionPattern; }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | Call createPatternParser
|
---|
379 | */
|
---|
380 | virtual void activateOptions(log4cxx::helpers::Pool& p);
|
---|
381 |
|
---|
382 | virtual void setOption(const LogString& option, const LogString& value);
|
---|
383 |
|
---|
384 | /**
|
---|
385 | The PatternLayout does not handle the throwable contained within
|
---|
386 | {@link spi::LoggingEvent LoggingEvents}. Thus, it returns
|
---|
387 | <code>true</code>.
|
---|
388 | */
|
---|
389 | virtual bool ignoresThrowable() const
|
---|
390 | { return true; }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | Produces a formatted string as specified by the conversion pattern.
|
---|
394 | */
|
---|
395 | virtual void format(LogString& output,
|
---|
396 | const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& pool) const;
|
---|
397 |
|
---|
398 | protected:
|
---|
399 | virtual log4cxx::pattern::PatternMap getFormatSpecifiers();
|
---|
400 | };
|
---|
401 | LOG4CXX_PTR_DEF(PatternLayout);
|
---|
402 | } // namespace log4cxx
|
---|
403 |
|
---|
404 | #if defined(_MSC_VER)
|
---|
405 | #pragma warning ( pop )
|
---|
406 | #endif
|
---|
407 |
|
---|
408 |
|
---|
409 | #endif //_LOG4CXX_PATTERN_LAYOUT_H
|
---|