1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20 import org.apache.log4j.spi.LoggingEvent;
21
22 import java.util.Iterator;
23 import java.util.Set;
24 import org.apache.log4j.helpers.*;
25
26
27 /***
28 * Able to handle the contents of the LoggingEvent's Property bundle and either
29 * output the entire contents of the properties in a similar format to the
30 * java.util.Hashtable.toString(), or to output the value of a specific key
31 * within the property bundle
32 * when this pattern converter has the option set.
33 *
34 * @author Paul Smith
35 * @author Ceki Gülcü
36 */
37 public final class PropertiesPatternConverter
38 extends LoggingEventPatternConverter {
39 /***
40 * Name of property to output.
41 */
42 private final String option;
43
44 /***
45 * Private constructor.
46 * @param options options, may be null.
47 */
48 private PropertiesPatternConverter(
49 final String[] options) {
50 super(
51 ((options != null) && (options.length > 0))
52 ? ("Property{" + options[0] + "}") : "Properties", "property");
53
54 if ((options != null) && (options.length > 0)) {
55 option = options[0];
56 } else {
57 option = null;
58 }
59 }
60
61 /***
62 * Obtains an instance of PropertiesPatternConverter.
63 * @param options options, may be null or first element contains name of property to format.
64 * @return instance of PropertiesPatternConverter.
65 */
66 public static PropertiesPatternConverter newInstance(
67 final String[] options) {
68 return new PropertiesPatternConverter(options);
69 }
70
71 /***
72 * {@inheritDoc}
73 */
74 public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
75
76
77 if (option == null) {
78 toAppendTo.append("{");
79
80 try {
81 Set keySet = MDCKeySetExtractor.INSTANCE.getPropertyKeySet(event);
82 if (keySet != null) {
83 for (Iterator i = keySet.iterator(); i.hasNext();) {
84 Object item = i.next();
85 Object val = event.getMDC(item.toString());
86 toAppendTo.append("{").append(item).append(",").append(val).append(
87 "}");
88 }
89 }
90 } catch(Exception ex) {
91 LogLog.error("Unexpected exception while extracting MDC keys", ex);
92 }
93
94 toAppendTo.append("}");
95 } else {
96
97 Object val = event.getMDC(option);
98
99 if (val != null) {
100 toAppendTo.append(val);
101 }
102 }
103 }
104 }