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.util.Map; 033 import java.util.concurrent.ConcurrentHashMap; 034 import java.util.concurrent.ConcurrentMap; 035 036 /** 037 * This Appender allows the logging event to be manipulated before it is processed by other Appenders. 038 */ 039 @Plugin(name = "Rewrite", type = "Core", elementType = "appender", printObject = true) 040 public final class RewriteAppender extends AbstractAppender { 041 private final Configuration config; 042 private final ConcurrentMap<String, AppenderControl> appenders = new ConcurrentHashMap<String, AppenderControl>(); 043 private final RewritePolicy rewritePolicy; 044 private final AppenderRef[] appenderRefs; 045 046 private RewriteAppender(final String name, final Filter filter, final boolean handleException, 047 final AppenderRef[] appenderRefs, final RewritePolicy rewritePolicy, 048 final Configuration config) { 049 super(name, filter, null, handleException); 050 this.config = config; 051 this.rewritePolicy = rewritePolicy; 052 this.appenderRefs = appenderRefs; 053 } 054 055 @Override 056 public void start() { 057 final Map<String, Appender<?>> map = config.getAppenders(); 058 for (final AppenderRef ref : appenderRefs) { 059 final String name = ref.getRef(); 060 final Appender appender = map.get(name); 061 if (appender != null) { 062 appenders.put(name, new AppenderControl(appender, ref.getLevel(), null)); 063 } else { 064 LOGGER.error("Appender " + ref + " cannot be located. Reference ignored"); 065 } 066 } 067 super.start(); 068 } 069 070 @Override 071 public void stop() { 072 super.stop(); 073 } 074 075 /** 076 * Modify the event and pass to the subordinate Appenders. 077 * @param event The LogEvent. 078 */ 079 public void append(LogEvent event) { 080 if (rewritePolicy != null) { 081 event = rewritePolicy.rewrite(event); 082 } 083 for (final AppenderControl control : appenders.values()) { 084 control.callAppender(event); 085 } 086 } 087 088 /** 089 * Create a RewriteAppender. 090 * @param name The name of the Appender. 091 * @param suppress If true, exceptions will be handled in the Appender. 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(@PluginAttr("name") final String name, 100 @PluginAttr("suppressExceptions") final String suppress, 101 @PluginElement("appender-ref") final AppenderRef[] appenderRefs, 102 @PluginConfiguration final Configuration config, 103 @PluginElement("rewritePolicy") final RewritePolicy rewritePolicy, 104 @PluginElement("filter") final Filter filter) { 105 106 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 107 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, handleExceptions, appenderRefs, rewritePolicy, config); 117 } 118 }