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 java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ConcurrentMap;
21  
22  import org.apache.logging.log4j.core.Appender;
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.LogEvent;
25  import org.apache.logging.log4j.core.appender.AbstractAppender;
26  import org.apache.logging.log4j.core.config.AppenderControl;
27  import org.apache.logging.log4j.core.config.AppenderRef;
28  import org.apache.logging.log4j.core.config.Configuration;
29  import org.apache.logging.log4j.core.config.plugins.Plugin;
30  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
31  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
32  import org.apache.logging.log4j.core.config.plugins.PluginElement;
33  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
34  import org.apache.logging.log4j.core.util.Booleans;
35  
36  /**
37   * This Appender allows the logging event to be manipulated before it is processed by other Appenders.
38   */
39  @Plugin(name = "Rewrite", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true)
40  public final class RewriteAppender extends AbstractAppender {
41  
42      private final Configuration config;
43      private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<>();
44      private final RewritePolicy rewritePolicy;
45      private final AppenderRef[] appenderRefs;
46  
47      private RewriteAppender(final String name, final Filter filter, final boolean ignoreExceptions,
48                              final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy,
49                              final Configuration config) {
50          super(name, filter, null, ignoreExceptions);
51          this.config = config;
52          this.rewritePolicy = rewritePolicy;
53          this.appenderRefs = appenderRefs;
54      }
55  
56      @Override
57      public void start() {
58          for (final AppenderRef ref : appenderRefs) {
59              final String name = ref.getRef();
60              final Appender appender = config.getAppender(name);
61              if (appender != null) {
62                  final Filter filter = appender instanceof AbstractAppender ?
63                      ((AbstractAppender) appender).getFilter() : null;
64                  appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
65              } else {
66                  LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
67              }
68          }
69          super.start();
70      }
71  
72      /**
73       * Modifies the event and pass to the subordinate Appenders.
74       * @param event The LogEvent.
75       */
76      @Override
77      public void append(LogEvent event) {
78          if (rewritePolicy != null) {
79              event = rewritePolicy.rewrite(event);
80          }
81          for (final AppenderControl control : appenders.values()) {
82              control.callAppender(event);
83          }
84      }
85  
86      /**
87       * Creates a RewriteAppender.
88       * @param name The name of the Appender.
89       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
90       *               they are propagated to the caller.
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(
99              @PluginAttribute("name") final String name,
100             @PluginAttribute("ignoreExceptions") final String ignore,
101             @PluginElement("AppenderRef") final AppenderRef[] appenderRefs,
102             @PluginConfiguration final Configuration config,
103             @PluginElement("RewritePolicy") final RewritePolicy rewritePolicy,
104             @PluginElement("Filter") final Filter filter) {
105 
106         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
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, ignoreExceptions, appenderRefs, rewritePolicy, config);
116     }
117 }