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