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.Map;
20  import java.util.concurrent.ConcurrentHashMap;
21  import java.util.concurrent.ConcurrentMap;
22  
23  import org.apache.logging.log4j.core.Appender;
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", elementType = "appender", printObject = true)
41  public final class RewriteAppender extends AbstractAppender {
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 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      @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 ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
95       *               they are propagated to the caller.
96       * @param appenderRefs An array of Appender names to call.
97       * @param config The Configuration.
98       * @param rewritePolicy The policy to use to modify the event.
99       * @param filter A Filter to filter events.
100      * @return The created RewriteAppender.
101      */
102     @PluginFactory
103     public static RewriteAppender createAppender(
104             @PluginAttribute("name") final String name,
105             @PluginAttribute("ignoreExceptions") final String ignore,
106             @PluginElement("AppenderRef") final AppenderRef[] appenderRefs,
107             @PluginConfiguration final Configuration config,
108             @PluginElement("RewritePolicy") final RewritePolicy rewritePolicy,
109             @PluginElement("Filter") final Filter filter) {
110 
111         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
112         if (name == null) {
113             LOGGER.error("No name provided for RewriteAppender");
114             return null;
115         }
116         if (appenderRefs == null) {
117             LOGGER.error("No appender references defined for RewriteAppender");
118             return null;
119         }
120         return new RewriteAppender(name, filter, ignoreExceptions, appenderRefs, rewritePolicy, config);
121     }
122 }