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.helpers.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          final Map<String, Appender> map = config.getAppenders();
59          for (final AppenderRef ref : appenderRefs) {
60              final String name = ref.getRef();
61              final Appender appender = map.get(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      @Override
74      public void stop() {
75          super.stop();
76      }
77  
78      /**
79       * Modify the event and pass to the subordinate Appenders.
80       * @param event The LogEvent.
81       */
82      @Override
83      public void append(LogEvent event) {
84          if (rewritePolicy != null) {
85              event = rewritePolicy.rewrite(event);
86          }
87          for (final AppenderControl control : appenders.values()) {
88              control.callAppender(event);
89          }
90      }
91  
92      /**
93       * Create a RewriteAppender.
94       * @param name The name of the Appender.
95       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
96       *               they are propagated to the caller.
97       * @param appenderRefs An array of Appender names to call.
98       * @param config The Configuration.
99       * @param rewritePolicy The policy to use to modify the event.
100      * @param filter A Filter to filter events.
101      * @return The created RewriteAppender.
102      */
103     @PluginFactory
104     public static RewriteAppender createAppender(
105             @PluginAttribute("name") final String name,
106             @PluginAttribute("ignoreExceptions") final String ignore,
107             @PluginElement("AppenderRef") final AppenderRef[] appenderRefs,
108             @PluginConfiguration final Configuration config,
109             @PluginElement("RewritePolicy") final RewritePolicy rewritePolicy,
110             @PluginElement("Filter") final Filter filter) {
111 
112         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
113         if (name == null) {
114             LOGGER.error("No name provided for RewriteAppender");
115             return null;
116         }
117         if (appenderRefs == null) {
118             LOGGER.error("No appender references defined for RewriteAppender");
119             return null;
120         }
121         return new RewriteAppender(name, filter, ignoreExceptions, appenderRefs, rewritePolicy, config);
122     }
123 }