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", printObject = true)
    40  public final class RewriteAppender extends AbstractAppender {
    41
    42      private static final long serialVersionUID = 1L;
    43
    44      private final Configuration config;
    45      private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<String, AppenderControl>();
    46      private final RewritePolicy rewritePolicy;
    47      private final AppenderRef[] appenderRefs;
    48
    49      private RewriteAppender(final String name, final Filter filter, final boolean ignoreExceptions,
    50                              final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy,
    51                              final Configuration config) {
    52          super(name, filter, null, ignoreExceptions);
    53          this.config = config;
    54          this.rewritePolicy = rewritePolicy;
    55          this.appenderRefs = appenderRefs;
    56      }
    57
    58      @Override
    59      public void start() {
    60          for (final AppenderRef ref : appenderRefs) {
    61              final String name = ref.getRef();
    62              final Appender appender = config.getAppender(name);
    63              if (appender != null) {
    64                  final Filter filter = appender instanceof AbstractAppender ?
    65                      ((AbstractAppender) appender).getFilter() : null;
    66                  appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
    67              } else {
    68                  LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
    69              }
    70          }
    71          super.start();
    72      }
    73
    74      @Override
    75      public void stop() {
    76          super.stop();
    77      }
    78
    79      /**
    80       * Modify the event and pass to the subordinate Appenders.
    81       * @param event The LogEvent.
    82       */
    83      @Override
    84      public void append(LogEvent event) {
    85          if (rewritePolicy != null) {
    86              event = rewritePolicy.rewrite(event);
    87          }
    88          for (final AppenderControl control : appenders.values()) {
    89              control.callAppender(event);
    90          }
    91      }
    92
    93      /**
    94       * Create a RewriteAppender.
    95       * @param name The name of the Appender.
    96       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
    97       *               they are propagated to the caller.
    98       * @param appenderRefs An array of Appender names to call.
    99       * @param config The Configuration.
    100      * @param rewritePolicy The policy to use to modify the event.
    101      * @param filter A Filter to filter events.
    102      * @return The created RewriteAppender.
    103      */
    104     @PluginFactory
    105     public static RewriteAppender createAppender(
    106             @PluginAttribute("name") final String name,
    107             @PluginAttribute("ignoreExceptions") final String ignore,
    108             @PluginElement("AppenderRef") final AppenderRef[] appenderRefs,
    109             @PluginConfiguration final Configuration config,
    110             @PluginElement("RewritePolicy") final RewritePolicy rewritePolicy,
    111             @PluginElement("Filter") final Filter filter) {
    112
    113         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    114         if (name == null) {
    115             LOGGER.error("No name provided for RewriteAppender");
    116             return null;
    117         }
    118         if (appenderRefs == null) {
    119             LOGGER.error("No appender references defined for RewriteAppender");
    120             return null;
    121         }
    122         return new RewriteAppender(name, filter, ignoreExceptions, appenderRefs, rewritePolicy, config);
    123     }
    124 }