PropertiesPatternConverter xref

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
    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&uuml;lc&uuml;
    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      // if there is no additional options, we output every single
    76      // Key/Value pair for the MDC in a similar format to Hashtable.toString()
    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        // otherwise they just want a single key output
    97        Object val = event.getMDC(option);
    98
    99        if (val != null) {
    100         toAppendTo.append(val);
    101       }
    102     }
    103   }
    104 }