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(String name, Filter filter, boolean handleException, AppenderRef[] appenderRefs, 047 RewritePolicy rewritePolicy, Configuration config) { 048 super(name, filter, null, handleException); 049 this.config = config; 050 this.rewritePolicy = rewritePolicy; 051 this.appenderRefs = appenderRefs; 052 } 053 054 @Override 055 public void start() { 056 Map<String, Appender> map = config.getAppenders(); 057 for (AppenderRef ref : appenderRefs) { 058 String name = ref.getRef(); 059 Appender appender = map.get(name); 060 if (appender != null) { 061 appenders.put(name, new AppenderControl(appender, ref.getLevel(), null)); 062 } else { 063 LOGGER.error("Appender " + ref + " cannot be located. Reference ignored"); 064 } 065 } 066 super.start(); 067 } 068 069 @Override 070 public void stop() { 071 super.stop(); 072 } 073 074 /** 075 * Modify the event and pass to the subordinate Appenders. 076 * @param event The LogEvent. 077 */ 078 public void append(LogEvent event) { 079 if (rewritePolicy != null) { 080 event = rewritePolicy.rewrite(event); 081 } 082 for (AppenderControl control : appenders.values()) { 083 control.callAppender(event); 084 } 085 } 086 087 /** 088 * Create a RewriteAppender. 089 * @param name The name of the Appender. 090 * @param suppress If true, exceptions will be handled in the Appender. 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(@PluginAttr("name") String name, 099 @PluginAttr("suppressExceptions") String suppress, 100 @PluginElement("appender-ref") AppenderRef[] appenderRefs, 101 @PluginConfiguration Configuration config, 102 @PluginElement("rewritePolicy") RewritePolicy rewritePolicy, 103 @PluginElement("filter") Filter filter) { 104 105 boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 106 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, handleExceptions, appenderRefs, rewritePolicy, config); 116 } 117 }