001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rewrite;
018
019import java.util.concurrent.ConcurrentHashMap;
020import java.util.concurrent.ConcurrentMap;
021
022import org.apache.logging.log4j.core.Appender;
023import org.apache.logging.log4j.core.Filter;
024import org.apache.logging.log4j.core.LogEvent;
025import org.apache.logging.log4j.core.appender.AbstractAppender;
026import org.apache.logging.log4j.core.config.AppenderControl;
027import org.apache.logging.log4j.core.config.AppenderRef;
028import org.apache.logging.log4j.core.config.Configuration;
029import org.apache.logging.log4j.core.config.plugins.Plugin;
030import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
031import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
032import org.apache.logging.log4j.core.config.plugins.PluginElement;
033import org.apache.logging.log4j.core.config.plugins.PluginFactory;
034import org.apache.logging.log4j.core.util.Booleans;
035
036/**
037 * This Appender allows the logging event to be manipulated before it is processed by other Appenders.
038 */
039@Plugin(name = "Rewrite", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true)
040public final class RewriteAppender extends AbstractAppender {
041
042    private final Configuration config;
043    private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<>();
044    private final RewritePolicy rewritePolicy;
045    private final AppenderRef[] appenderRefs;
046
047    private RewriteAppender(final String name, final Filter filter, final boolean ignoreExceptions,
048                            final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy,
049                            final Configuration config) {
050        super(name, filter, null, ignoreExceptions);
051        this.config = config;
052        this.rewritePolicy = rewritePolicy;
053        this.appenderRefs = appenderRefs;
054    }
055
056    @Override
057    public void start() {
058        for (final AppenderRef ref : appenderRefs) {
059            final String name = ref.getRef();
060            final Appender appender = config.getAppender(name);
061            if (appender != null) {
062                final Filter filter = appender instanceof AbstractAppender ?
063                    ((AbstractAppender) appender).getFilter() : null;
064                appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
065            } else {
066                LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
067            }
068        }
069        super.start();
070    }
071
072    /**
073     * Modifies the event and pass to the subordinate Appenders.
074     * @param event The LogEvent.
075     */
076    @Override
077    public void append(LogEvent event) {
078        if (rewritePolicy != null) {
079            event = rewritePolicy.rewrite(event);
080        }
081        for (final AppenderControl control : appenders.values()) {
082            control.callAppender(event);
083        }
084    }
085
086    /**
087     * Creates a RewriteAppender.
088     * @param name The name of the Appender.
089     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
090     *               they are propagated to the caller.
091     * @param appenderRefs An array of Appender names to call.
092     * @param config The Configuration.
093     * @param rewritePolicy The policy to use to modify the event.
094     * @param filter A Filter to filter events.
095     * @return The created RewriteAppender.
096     */
097    @PluginFactory
098    public static RewriteAppender createAppender(
099            @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}