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.Core;
024import org.apache.logging.log4j.core.Filter;
025import org.apache.logging.log4j.core.LogEvent;
026import org.apache.logging.log4j.core.appender.AbstractAppender;
027import org.apache.logging.log4j.core.config.AppenderControl;
028import org.apache.logging.log4j.core.config.AppenderRef;
029import org.apache.logging.log4j.core.config.Configuration;
030import org.apache.logging.log4j.core.config.plugins.Plugin;
031import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
032import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
033import org.apache.logging.log4j.core.config.plugins.PluginElement;
034import org.apache.logging.log4j.core.config.plugins.PluginFactory;
035import org.apache.logging.log4j.core.util.Booleans;
036
037/**
038 * This Appender allows the logging event to be manipulated before it is processed by other Appenders.
039 */
040@Plugin(name = "Rewrite", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
041public final class RewriteAppender extends AbstractAppender {
042
043    private final Configuration config;
044    private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<>();
045    private final RewritePolicy rewritePolicy;
046    private final AppenderRef[] appenderRefs;
047
048    private RewriteAppender(final String name, final Filter filter, final boolean ignoreExceptions,
049                            final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy,
050                            final Configuration config) {
051        super(name, filter, null, ignoreExceptions);
052        this.config = config;
053        this.rewritePolicy = rewritePolicy;
054        this.appenderRefs = appenderRefs;
055    }
056
057    @Override
058    public void start() {
059        for (final AppenderRef ref : appenderRefs) {
060            final String name = ref.getRef();
061            final Appender appender = config.getAppender(name);
062            if (appender != null) {
063                final Filter filter = appender instanceof AbstractAppender ?
064                    ((AbstractAppender) appender).getFilter() : null;
065                appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
066            } else {
067                LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
068            }
069        }
070        super.start();
071    }
072
073    /**
074     * Modifies the event and pass to the subordinate Appenders.
075     * @param event The LogEvent.
076     */
077    @Override
078    public void append(LogEvent event) {
079        if (rewritePolicy != null) {
080            event = rewritePolicy.rewrite(event);
081        }
082        for (final AppenderControl control : appenders.values()) {
083            control.callAppender(event);
084        }
085    }
086
087    /**
088     * Creates a RewriteAppender.
089     * @param name The name of the Appender.
090     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
091     *               they are propagated to the caller.
092     * @param appenderRefs An array of Appender names to call.
093     * @param config The Configuration.
094     * @param rewritePolicy The policy to use to modify the event.
095     * @param filter A Filter to filter events.
096     * @return The created RewriteAppender.
097     */
098    @PluginFactory
099    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}