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 org.apache.logging.log4j.core.LogEvent;
20  import org.apache.logging.log4j.core.config.plugins.Plugin;
21  import org.apache.logging.log4j.message.MapMessage;
22  import org.apache.logging.log4j.message.MapMessage.MapFormat;
23  
24  import java.util.Objects;
25  
26  /**
27   * Able to handle the contents of the LogEvent's MapMessage 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 Map.
31   */
32  @Plugin(name = "MapPatternConverter", category = PatternConverter.CATEGORY)
33  @ConverterKeys({ "K", "map", "MAP" })
34  public final class MapPatternConverter extends LogEventPatternConverter {
35  
36      private static final String JAVA_UNQUOTED = MapFormat.JAVA_UNQUOTED.name();
37  
38      /**
39       * Name of property to output.
40       */
41      private final String key;
42  
43      /**
44       * Format to use when no key is provided.
45       *
46       * @see MapFormat
47       * @since 2.11.2
48       */
49      private final String[] format;
50  
51      /**
52       * Private constructor.
53       *
54       * @param options options, may be null.
55       */
56      private MapPatternConverter(final String[] options, String... format) {
57          super(options != null && options.length > 0 ? "MAP{" + options[0] + '}' : "MAP", "map");
58          key = options != null && options.length > 0 ? options[0] : null;
59          this.format = format;
60      }
61  
62      /**
63       * Obtains an instance of {@link MapPatternConverter}.
64       *
65       * @param options options, may be null or first element contains name of property to format.
66       * @return instance of {@link MapPatternConverter}.
67       */
68      public static MapPatternConverter newInstance(final String[] options) {
69          return new MapPatternConverter(options, JAVA_UNQUOTED);
70      }
71  
72      /**
73       * Obtain an instance of {@link MapPatternConverter}.
74       *
75       * @param options options, may be null or first element contains name of property to format.
76       * @param format the format to use if no options are given (i.e., options is null). Ignored if options is non-null.
77       * @return instance of {@link MapPatternConverter}.
78       * @since 2.11.2
79       */
80      public static MapPatternConverter newInstance(final String[] options, final MapFormat format) {
81          return new MapPatternConverter(options, Objects.toString(format, JAVA_UNQUOTED));
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void format(final LogEvent event, final StringBuilder toAppendTo) {
89          MapMessage msg;
90          if (event.getMessage() instanceof MapMessage) {
91              msg = (MapMessage) event.getMessage();
92          } else {
93              return;
94          }
95          // if there is no additional options, we output every single
96          // Key/Value pair for the Map in a similar format to Hashtable.toString()
97          if (key == null) {
98              msg.formatTo(format, toAppendTo);
99          } else {
100             // otherwise they just want a single key output
101             final String val = msg.get(key);
102 
103             if (val != null) {
104                 toAppendTo.append(val);
105             }
106         }
107     }
108 }