View Javadoc
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  package org.apache.logging.log4j.core.pattern;
18  
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  
26  /**
27   * Able to handle the contents of the LogEvent's MDC and either
28   * output the entire contents of the properties in a similar format to the
29   * java.util.Hashtable.toString(), or to output the value of a specific key
30   * within the property bundle
31   * when this pattern converter has the option set.
32   */
33  @Plugin(name = "MdcPatternConverter", category = PatternConverter.CATEGORY)
34  @ConverterKeys({ "X", "mdc", "MDC" })
35  public final class MdcPatternConverter extends LogEventPatternConverter {
36      /**
37       * Name of property to output.
38       */
39      private final String key;
40      private final String[] keys;
41      private final boolean full;
42  
43      /**
44       * Private constructor.
45       *
46       * @param options options, may be null.
47       */
48      private MdcPatternConverter(final String[] options) {
49          super(options != null && options.length > 0 ? "MDC{" + options[0] + '}' : "MDC", "mdc");
50          if (options != null && options.length > 0) {
51              full = false;
52              if (options[0].indexOf(',') > 0) {
53                  keys = options[0].split(",");
54                  key = null;
55              } else {
56                  keys = null;
57                  key = options[0];
58              }
59          } else {
60              full = true;
61              key = null;
62              keys = null;
63          }
64      }
65  
66      /**
67       * Obtains an instance of PropertiesPatternConverter.
68       *
69       * @param options options, may be null or first element contains name of property to format.
70       * @return instance of PropertiesPatternConverter.
71       */
72      public static MdcPatternConverter newInstance(final String[] options) {
73          return new MdcPatternConverter(options);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void format(final LogEvent event, final StringBuilder toAppendTo) {
81          final Map<String, String> contextMap = event.getContextMap();
82          // if there is no additional options, we output every single
83          // Key/Value pair for the MDC in a similar format to Hashtable.toString()
84          if (full) {
85              if (contextMap == null || contextMap.isEmpty()) {
86                  toAppendTo.append("{}");
87                  return;
88              }
89              final StringBuilder sb = new StringBuilder("{");
90              final Set<String> eventKeys = new TreeSet<>(contextMap.keySet());
91              for (final String eventKey : eventKeys) {
92                  if (sb.length() > 1) {
93                      sb.append(", ");
94                  }
95                  sb.append(eventKey).append('=').append(contextMap.get(eventKey));
96  
97              }
98              sb.append('}');
99              toAppendTo.append(sb);
100         } else {
101             if (keys != null) {
102                 if (contextMap == null || contextMap.isEmpty()) {
103                     toAppendTo.append("{}");
104                     return;
105                 }
106                 // Print all the keys in the array that have a value.
107                 final StringBuilder sb = new StringBuilder("{");
108                 for (String key : keys) {
109                     key = key.trim();
110                     if (contextMap.containsKey(key)) {
111                         if (sb.length() > 1) {
112                             sb.append(", ");
113                         }
114                         sb.append(key).append('=').append(contextMap.get(key));
115                     }
116                 }
117                 sb.append('}');
118                 toAppendTo.append(sb);
119             } else if (contextMap != null){
120                 // otherwise they just want a single key output
121                 final Object val = contextMap.get(key);
122 
123                 if (val != null) {
124                     toAppendTo.append(val);
125                 }
126             }
127         }
128     }
129 }