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