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.HashMap;
23  import java.util.Map;
24  
25  /**
26   * This policy rewrites events where the message of the
27   * original event implementes java.util.Map.
28   * All other events are passed through unmodified.
29   * If the map contains a "message" entry, the value will be
30   * used as the message for the rewritten event.  The rewritten
31   * event will have a property set that is the combination of the
32   * original property set and the other members of the message map.
33   * If both the original property set and the message map
34   * contain the same entry, the value from the message map
35   * will overwrite the original property set.
36   * <p>
37   * The combination of the RewriteAppender and this policy
38   * performs the same actions as the MapFilter from log4j 1.3.
39   */
40  public class MapRewritePolicy implements RewritePolicy {
41      /**
42       * {@inheritDoc}
43       */
44      public LoggingEvent rewrite(final LoggingEvent source) {
45          Object msg = source.getMessage();
46          if (msg instanceof Map) {
47              Map props = new HashMap(source.getProperties());
48              Map eventProps = (Map) msg;
49              //
50              //   if the map sent in the logging request
51              //      has "message" entry, use that as the message body
52              //      otherwise, use the entire map.
53              //
54              Object newMsg = eventProps.get("message");
55              if (newMsg == null) {
56                  newMsg = msg;
57              }
58  
59              for (Object o : eventProps.entrySet()) {
60                  Map.Entry entry = (Map.Entry) o;
61                  if (!("message".equals(entry.getKey()))) {
62                      props.put(entry.getKey(), entry.getValue());
63                  }
64              }
65  
66              return new LoggingEvent(
67                  source.getFQNOfLoggerClass(),
68                  source.getLogger() != null ? source.getLogger() : Logger.getLogger(source.getLoggerName()),
69                  source.getTimeStamp(),
70                  source.getLevel(),
71                  newMsg,
72                  source.getThreadName(),
73                  source.getThrowableInformation(),
74                  source.getNDC(),
75                  source.getLocationInformation(),
76                  props);
77          } else {
78              return source;
79          }
80  
81      }
82  }