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
    41      /**
    42       * Private constructor.
    43       *
    44       * @param options options, may be null.
    45       */
    46      private MdcPatternConverter(final String[] options) {
    47          super(options != null && options.length > 0 ? "MDC{" + options[0] + '}' : "MDC", "mdc");
    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 MdcPatternConverter newInstance(final String[] options) {
    58          return new MdcPatternConverter(options);
    59      }
    60
    61      /**
    62       * {@inheritDoc}
    63       */
    64      @Override
    65      public void format(final LogEvent event, final StringBuilder toAppendTo) {
    66          final Map<String, String> contextMap = event.getContextMap();
    67          // if there is no additional options, we output every single
    68          // Key/Value pair for the MDC in a similar format to Hashtable.toString()
    69          if (key == null) {
    70
    71
    72              if (contextMap == null || contextMap.isEmpty()) {
    73                  toAppendTo.append("{}");
    74                  return;
    75              }
    76              final StringBuilder sb = new StringBuilder("{");
    77              final Set<String> keys = new TreeSet<String>(contextMap.keySet());
    78              for (final String key : keys) {
    79                  if (sb.length() > 1) {
    80                      sb.append(", ");
    81                  }
    82                  sb.append(key).append('=').append(contextMap.get(key));
    83
    84              }
    85              sb.append('}');
    86              toAppendTo.append(sb);
    87          } else if (contextMap != null) {
    88              // otherwise they just want a single key output
    89              final Object val = contextMap.get(key);
    90
    91              if (val != null) {
    92                  toAppendTo.append(val);
    93              }
    94          }
    95      }
    96  }