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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.log4j.rewrite;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
23 import java.util.StringTokenizer;
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.spi.LoggingEvent;
29 * This policy rewrites events by adding
30 * a user-specified list of properties to the event.
31 * Existing properties are not modified.
33 * The combination of the RewriteAppender and this policy
34 * performs the same actions as the PropertyFilter from log4j 1.3.
37 public class PropertyRewritePolicy implements RewritePolicy {
38 private Map properties = Collections.EMPTY_MAP;
39 public PropertyRewritePolicy() {
43 * Set a string representing the property name/value pairs.
45 * Form: propname1=propvalue1,propname2=propvalue2
49 public void setProperties(String props) {
50 Map 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());
57 properties = hashTable;
64 public LoggingEvent rewrite(final LoggingEvent source) {
65 if (!properties.isEmpty()) {
66 Map rewriteProps = new HashMap(source.getProperties());
67 for(Iterator iter = properties.entrySet().iterator();
70 Map.Entry entry = (Map.Entry) iter.next();
71 if (!rewriteProps.containsKey(entry.getKey())) {
72 rewriteProps.put(entry.getKey(), entry.getValue());
76 return new LoggingEvent(
77 source.getFQNOfLoggerClass(),
78 source.getLogger() != null ? source.getLogger(): Logger.getLogger(source.getLoggerName()),
79 source.getTimeStamp(),
82 source.getThreadName(),
83 source.getThrowableInformation(),
85 source.getLocationInformation(),