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.log4j.rewrite;
    18
    19  import org.apache.log4j.Logger;
    20  import org.apache.log4j.spi.LoggingEvent;
    21
    22  import java.util.Collections;
    23  import java.util.HashMap;
    24  import java.util.Map;
    25  import java.util.StringTokenizer;
    26
    27  /**
    28   * This policy rewrites events by adding
    29   * a user-specified list of properties to the event.
    30   * Existing properties are not modified.
    31   * <p>
    32   * The combination of the RewriteAppender and this policy
    33   * performs the same actions as the PropertyFilter from log4j 1.3.
    34   */
    35
    36  public class PropertyRewritePolicy implements RewritePolicy {
    37      private Map properties = Collections.EMPTY_MAP;
    38
    39      public PropertyRewritePolicy() {
    40      }
    41
    42      /**
    43       * Set a string representing the property name/value pairs.
    44       * <p>
    45       * Form: propname1=propvalue1,propname2=propvalue2
    46       *
    47       * @param props
    48       */
    49      public void setProperties(String props) {
    50          Map<String, String> hashTable = new HashMap<>();
    51          StringTokenizer pairs = new StringTokenizer(props, ",");
    52          while (pairs.hasMoreTokens()) {
    53              StringTokenizer entry = new StringTokenizer(pairs.nextToken(), "=");
    54              hashTable.put(entry.nextElement().toString().trim(), entry.nextElement().toString().trim());
    55          }
    56          synchronized (this) {
    57              properties = hashTable;
    58          }
    59      }
    60
    61      /**
    62       * {@inheritDoc}
    63       */
    64      public LoggingEvent rewrite(final LoggingEvent source) {
    65          if (!properties.isEmpty()) {
    66              Map rewriteProps = new HashMap(source.getProperties());
    67              for (Object o : properties.entrySet()) {
    68                  Map.Entry entry = (Map.Entry) o;
    69                  if (!rewriteProps.containsKey(entry.getKey())) {
    70                      rewriteProps.put(entry.getKey(), entry.getValue());
    71                  }
    72              }
    73
    74              return new LoggingEvent(
    75                  source.getFQNOfLoggerClass(),
    76                  source.getLogger() != null ? source.getLogger() : Logger.getLogger(source.getLoggerName()),
    77                  source.getTimeStamp(),
    78                  source.getLevel(),
    79                  source.getMessage(),
    80                  source.getThreadName(),
    81                  source.getThrowableInformation(),
    82                  source.getNDC(),
    83                  source.getLocationInformation(),
    84                  rewriteProps);
    85          }
    86          return source;
    87      }
    88
    89
    90  }