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.HashMap;
20 import java.util.Iterator;
23 import org.apache.log4j.Logger;
24 import org.apache.log4j.spi.LoggingEvent;
27 * This policy rewrites events where the message of the
28 * original event implementes java.util.Map.
29 * All other events are passed through unmodified.
30 * If the map contains a "message" entry, the value will be
31 * used as the message for the rewritten event. The rewritten
32 * event will have a property set that is the combination of the
33 * original property set and the other members of the message map.
34 * If both the original property set and the message map
35 * contain the same entry, the value from the message map
36 * will overwrite the original property set.
38 * The combination of the RewriteAppender and this policy
39 * performs the same actions as the MapFilter from log4j 1.3.
41 public class MapRewritePolicy implements RewritePolicy {
45 public LoggingEvent rewrite(final LoggingEvent source) {
46 Object msg = source.getMessage();
47 if (msg instanceof Map) {
48 Map props = new HashMap(source.getProperties());
49 Map eventProps = (Map) msg;
51 // if the map sent in the logging request
52 // has "message" entry, use that as the message body
53 // otherwise, use the entire map.
55 Object newMsg = eventProps.get("message");
60 for(Iterator iter = eventProps.entrySet().iterator();
63 Map.Entry entry = (Map.Entry) iter.next();
64 if (!("message".equals(entry.getKey()))) {
65 props.put(entry.getKey(), entry.getValue());
69 return new LoggingEvent(
70 source.getFQNOfLoggerClass(),
71 source.getLogger() != null ? source.getLogger(): Logger.getLogger(source.getLoggerName()),
72 source.getTimeStamp(),
75 source.getThreadName(),
76 source.getThrowableInformation(),
78 source.getLocationInformation(),