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(final String name, final Filter filter, final boolean handleException,
47                              final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy,
48                              final Configuration config) {
49          super(name, filter, null, handleException);
50          this.config = config;
51          this.rewritePolicy = rewritePolicy;
52          this.appenderRefs = appenderRefs;
53      }
54  
55      @Override
56      public void start() {
57          final Map<String, Appender<?>> map = config.getAppenders();
58          for (final AppenderRef ref : appenderRefs) {
59              final String name = ref.getRef();
60              final Appender appender = map.get(name);
61              if (appender != null) {
62                  appenders.put(name, new AppenderControl(appender, ref.getLevel(), null));
63              } else {
64                  LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
65              }
66          }
67          super.start();
68      }
69  
70      @Override
71      public void stop() {
72          super.stop();
73      }
74  
75      /**
76       * Modify the event and pass to the subordinate Appenders.
77       * @param event The LogEvent.
78       */
79      public void append(LogEvent event) {
80          if (rewritePolicy != null) {
81              event = rewritePolicy.rewrite(event);
82          }
83          for (final AppenderControl control : appenders.values()) {
84              control.callAppender(event);
85          }
86      }
87  
88      /**
89       * Create a RewriteAppender.
90       * @param name The name of the Appender.
91       * @param suppress If true, exceptions will be handled in the Appender.
92       * @param appenderRefs An array of Appender names to call.
93       * @param config The Configuration.
94       * @param rewritePolicy The policy to use to modify the event.
95       * @param filter A Filter to filter events.
96       * @return The created RewriteAppender.
97       */
98      @PluginFactory
99      public static RewriteAppender createAppender(@PluginAttr("name") final String name,
100                                           @PluginAttr("suppressExceptions") final String suppress,
101                                           @PluginElement("appender-ref") final AppenderRef[] appenderRefs,
102                                           @PluginConfiguration final Configuration config,
103                                           @PluginElement("rewritePolicy") final RewritePolicy rewritePolicy,
104                                           @PluginElement("filter") final Filter filter) {
105 
106         final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
107 
108         if (name == null) {
109             LOGGER.error("No name provided for RewriteAppender");
110             return null;
111         }
112         if (appenderRefs == null) {
113             LOGGER.error("No appender references defined for RewriteAppender");
114             return null;
115         }
116         return new RewriteAppender(name, filter, handleExceptions, appenderRefs, rewritePolicy, config);
117     }
118 }