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  import org.apache.logging.log4j.message.MapMessage;
26  
27  /**
28   * Able to handle the contents of the LogEvent's MapMessage 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 Map.
32   */
33  @Plugin(name = "MapPatternConverter", category = "Converter")
34  @ConverterKeys({"K", "map", "MAP" })
35  public final class MapPatternConverter extends LogEventPatternConverter {
36      /**
37       * Name of property to output.
38       */
39      private final String key;
40  
41      /**
42       * Private constructor.
43       *
44       * @param options options, may be null.
45       */
46      private MapPatternConverter(final String[] options) {
47          super(options != null && options.length > 0 ? "MAP{" + options[0] + "}" : "MAP", "map");
48          key = options != null && options.length > 0 ? options[0] : null;
49      }
50  
51      /**
52       * Obtains an instance of PropertiesPatternConverter.
53       *
54       * @param options options, may be null or first element contains name of property to format.
55       * @return instance of PropertiesPatternConverter.
56       */
57      public static MapPatternConverter newInstance(final String[] options) {
58          return new MapPatternConverter(options);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void format(final LogEvent event, final StringBuilder toAppendTo) {
66          MapMessage msg;
67          if (event.getMessage() instanceof MapMessage) {
68              msg = (MapMessage) event.getMessage();
69          } else {
70              return;
71          }
72          final Map<String, String> map = msg.getData();
73          // if there is no additional options, we output every single
74          // Key/Value pair for the Map in a similar format to Hashtable.toString()
75          if (key == null) {
76              if (map.size() == 0) {
77                  toAppendTo.append("{}");
78                  return;
79              }
80              final StringBuilder sb = new StringBuilder("{");
81              final Set<String> keys = new TreeSet<String>(map.keySet());
82              for (final String key : keys) {
83                  if (sb.length() > 1) {
84                      sb.append(", ");
85                  }
86                  sb.append(key).append("=").append(map.get(key));
87  
88              }
89              sb.append("}");
90              toAppendTo.append(sb);
91          } else {
92              // otherwise they just want a single key output
93              final String val = map.get(key);
94  
95              if (val != null) {
96                  toAppendTo.append(val);
97              }
98          }
99      }
100 }