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 java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.apache.log4j.Logger;
24  import org.apache.log4j.spi.LoggingEvent;
25  
26  /**
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.
37   *
38   * The combination of the RewriteAppender and this policy
39   * performs the same actions as the MapFilter from log4j 1.3. 
40   */
41  public class MapRewritePolicy implements RewritePolicy {
42      /**
43       * {@inheritDoc}
44       */
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;
50              //
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.
54              //
55              Object newMsg = eventProps.get("message");
56              if (newMsg == null) {
57                  newMsg = msg;
58              }
59  
60              for(Iterator iter = eventProps.entrySet().iterator();
61                      iter.hasNext();
62                    ) {
63                  Map.Entry entry = (Map.Entry) iter.next();
64                  if (!("message".equals(entry.getKey()))) {
65                      props.put(entry.getKey(), entry.getValue());
66                  }
67              }
68  
69              return new LoggingEvent(
70                      source.getFQNOfLoggerClass(),
71                      source.getLogger() != null ? source.getLogger(): Logger.getLogger(source.getLoggerName()), 
72                      source.getTimeStamp(),
73                      source.getLevel(),
74                      newMsg,
75                      source.getThreadName(),
76                      source.getThrowableInformation(),
77                      source.getNDC(),
78                      source.getLocationInformation(),
79                      props);
80          } else {
81              return source;
82          }
83  
84      }
85  }