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     */
017    package org.apache.logging.log4j.core.appender.rewrite;
018    
019    import org.apache.logging.log4j.core.Appender;
020    import org.apache.logging.log4j.core.Filter;
021    import org.apache.logging.log4j.core.LogEvent;
022    import org.apache.logging.log4j.core.appender.AbstractAppender;
023    import org.apache.logging.log4j.core.config.AppenderControl;
024    import org.apache.logging.log4j.core.config.AppenderRef;
025    import org.apache.logging.log4j.core.config.Configuration;
026    import org.apache.logging.log4j.core.config.plugins.Plugin;
027    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
028    import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
029    import org.apache.logging.log4j.core.config.plugins.PluginElement;
030    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
031    
032    import java.io.Serializable;
033    import java.util.Map;
034    import java.util.concurrent.ConcurrentHashMap;
035    import java.util.concurrent.ConcurrentMap;
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", elementType = "appender", printObject = true)
041    public final class RewriteAppender<T extends Serializable> extends AbstractAppender<T> {
042        private final Configuration config;
043        private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<String, AppenderControl>();
044        private final RewritePolicy rewritePolicy;
045        private final AppenderRef[] appenderRefs;
046    
047        private RewriteAppender(final String name, final Filter filter, final boolean handleException,
048                                final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy,
049                                final Configuration config) {
050            super(name, filter, null, handleException);
051            this.config = config;
052            this.rewritePolicy = rewritePolicy;
053            this.appenderRefs = appenderRefs;
054        }
055    
056        @Override
057        @SuppressWarnings("unchecked")
058        public void start() {
059            final Map<String, Appender<?>> map = config.getAppenders();
060            for (final AppenderRef ref : appenderRefs) {
061                final String name = ref.getRef();
062                final Appender appender = map.get(name);
063                if (appender != null) {
064                    Filter filter = appender instanceof AbstractAppender<?> ?
065                        ((AbstractAppender<?>) appender).getFilter() : null;
066                    appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
067                } else {
068                    LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
069                }
070            }
071            super.start();
072        }
073    
074        @Override
075        public void stop() {
076            super.stop();
077        }
078    
079        /**
080         * Modify the event and pass to the subordinate Appenders.
081         * @param event The LogEvent.
082         */
083        @Override
084        public void append(LogEvent event) {
085            if (rewritePolicy != null) {
086                event = rewritePolicy.rewrite(event);
087            }
088            for (final AppenderControl control : appenders.values()) {
089                control.callAppender(event);
090            }
091        }
092    
093        /**
094         * Create a RewriteAppender.
095         * @param name The name of the Appender.
096         * @param suppress If true, exceptions will be handled in the Appender.
097         * @param appenderRefs An array of Appender names to call.
098         * @param config The Configuration.
099         * @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 <S extends Serializable> RewriteAppender<S> createAppender(@PluginAttr("name") final String name,
105                                              @PluginAttr("suppressExceptions") final String suppress,
106                                              @PluginElement("appender-ref") final AppenderRef[] appenderRefs,
107                                              @PluginConfiguration final Configuration config,
108                                              @PluginElement("rewritePolicy") final RewritePolicy rewritePolicy,
109                                              @PluginElement("filter") final Filter filter) {
110    
111            final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
112    
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<S>(name, filter, handleExceptions, appenderRefs, rewritePolicy, config);
122        }
123    }