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.logging.log4j.core.appender.rewrite;
18  
19  import org.apache.logging.log4j.core.Appender;
20  import org.apache.logging.log4j.core.Filter;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.appender.AbstractAppender;
23  import org.apache.logging.log4j.core.config.AppenderControl;
24  import org.apache.logging.log4j.core.config.AppenderRef;
25  import org.apache.logging.log4j.core.config.Configuration;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
28  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
29  import org.apache.logging.log4j.core.config.plugins.PluginElement;
30  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
31  
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  import java.util.concurrent.ConcurrentMap;
35  
36  /**
37   * This Appender allows the logging event to be manipulated before it is processed by other Appenders.
38   */
39  @Plugin(name = "Rewrite", type = "Core", elementType = "appender", printObject = true)
40  public final class RewriteAppender extends AbstractAppender {
41      private final Configuration config;
42      private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<String, AppenderControl>();
43      private final RewritePolicy rewritePolicy;
44      private final AppenderRef[] appenderRefs;
45  
46      private RewriteAppender(String name, Filter filter, boolean handleException, AppenderRef[] appenderRefs,
47                              RewritePolicy rewritePolicy, Configuration config) {
48          super(name, filter, null, handleException);
49          this.config = config;
50          this.rewritePolicy = rewritePolicy;
51          this.appenderRefs = appenderRefs;
52      }
53  
54      @Override
55      public void start() {
56          Map<String, Appender> map = config.getAppenders();
57          for (AppenderRef ref : appenderRefs) {
58              String name = ref.getRef();
59              Appender appender = map.get(name);
60              if (appender != null) {
61                  appenders.put(name, new AppenderControl(appender, ref.getLevel(), null));
62              } else {
63                  LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
64              }
65          }
66          super.start();
67      }
68  
69      @Override
70      public void stop() {
71          super.stop();
72      }
73  
74      /**
75       * Modify the event and pass to the subordinate Appenders.
76       * @param event The LogEvent.
77       */
78      public void append(LogEvent event) {
79          if (rewritePolicy != null) {
80              event = rewritePolicy.rewrite(event);
81          }
82          for (AppenderControl control : appenders.values()) {
83              control.callAppender(event);
84          }
85      }
86  
87      /**
88       * Create a RewriteAppender.
89       * @param name The name of the Appender.
90       * @param suppress If true, exceptions will be handled in the Appender.
91       * @param appenderRefs An array of Appender names to call.
92       * @param config The Configuration.
93       * @param rewritePolicy The policy to use to modify the event.
94       * @param filter A Filter to filter events.
95       * @return The created RewriteAppender.
96       */
97      @PluginFactory
98      public static RewriteAppender createAppender(@PluginAttr("name") String name,
99                                            @PluginAttr("suppressExceptions") String suppress,
100                                           @PluginElement("appender-ref") AppenderRef[] appenderRefs,
101                                           @PluginConfiguration Configuration config,
102                                           @PluginElement("rewritePolicy") RewritePolicy rewritePolicy,
103                                           @PluginElement("filter") Filter filter) {
104 
105         boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
106 
107         if (name == null) {
108             LOGGER.error("No name provided for RewriteAppender");
109             return null;
110         }
111         if (appenderRefs == null) {
112             LOGGER.error("No appender references defined for RewriteAppender");
113             return null;
114         }
115         return new RewriteAppender(name, filter, handleExceptions, appenderRefs, rewritePolicy, config);
116     }
117 }