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 appenders.put(name, new AppenderControl(appender, ref.getLevel(), null));
065 } else {
066 LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
067 }
068 }
069 super.start();
070 }
071
072 @Override
073 public void stop() {
074 super.stop();
075 }
076
077 /**
078 * Modify the event and pass to the subordinate Appenders.
079 * @param event The LogEvent.
080 */
081 @Override
082 public void append(LogEvent event) {
083 if (rewritePolicy != null) {
084 event = rewritePolicy.rewrite(event);
085 }
086 for (final AppenderControl control : appenders.values()) {
087 control.callAppender(event);
088 }
089 }
090
091 /**
092 * Create a RewriteAppender.
093 * @param name The name of the Appender.
094 * @param suppress If true, exceptions will be handled in the Appender.
095 * @param appenderRefs An array of Appender names to call.
096 * @param config The Configuration.
097 * @param rewritePolicy The policy to use to modify the event.
098 * @param filter A Filter to filter events.
099 * @return The created RewriteAppender.
100 */
101 @PluginFactory
102 public static <S extends Serializable> RewriteAppender<S> createAppender(@PluginAttr("name") final String name,
103 @PluginAttr("suppressExceptions") final String suppress,
104 @PluginElement("appender-ref") final AppenderRef[] appenderRefs,
105 @PluginConfiguration final Configuration config,
106 @PluginElement("rewritePolicy") final RewritePolicy rewritePolicy,
107 @PluginElement("filter") final Filter filter) {
108
109 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
110
111 if (name == null) {
112 LOGGER.error("No name provided for RewriteAppender");
113 return null;
114 }
115 if (appenderRefs == null) {
116 LOGGER.error("No appender references defined for RewriteAppender");
117 return null;
118 }
119 return new RewriteAppender<S>(name, filter, handleExceptions, appenderRefs, rewritePolicy, config);
120 }
121 }