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.Logger; 020 import org.apache.logging.log4j.core.LogEvent; 021 import org.apache.logging.log4j.core.config.Configuration; 022 import org.apache.logging.log4j.core.config.Property; 023 import org.apache.logging.log4j.core.config.plugins.Plugin; 024 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 025 import org.apache.logging.log4j.core.config.plugins.PluginElement; 026 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 027 import org.apache.logging.log4j.core.impl.Log4jLogEvent; 028 import org.apache.logging.log4j.status.StatusLogger; 029 030 import java.util.Arrays; 031 import java.util.HashMap; 032 import java.util.List; 033 import java.util.Map; 034 035 /** 036 * This policy modifies events by replacing or possibly adding keys and values to the MapMessage. 037 */ 038 @Plugin(name = "PropertiesRewritePolicy", type = "Core", elementType = "rewritePolicy", printObject = true) 039 public final class PropertiesRewritePolicy implements RewritePolicy { 040 /** 041 * Allow subclasses access to the status logger without creating another instance. 042 */ 043 protected static final Logger LOGGER = StatusLogger.getLogger(); 044 045 private final Map<Property, Boolean> properties; 046 047 private final Configuration config; 048 049 private PropertiesRewritePolicy(Configuration config, List<Property> props) { 050 this.config = config; 051 this.properties = new HashMap<Property, Boolean>(props.size()); 052 for (Property prop : props) { 053 boolean interpolate = prop.getValue().contains("${"); 054 properties.put(prop, interpolate); 055 } 056 } 057 058 /** 059 * Rewrite the event. 060 * @param source a logging event that may be returned or 061 * used to create a new logging event. 062 * @return The LogEvent after rewriting. 063 */ 064 public LogEvent rewrite(LogEvent source) { 065 Map<String, String> props = new HashMap<String, String>(source.getContextMap()); 066 for (Map.Entry<Property, Boolean> entry : properties.entrySet()) { 067 Property prop = entry.getKey(); 068 if (!props.containsKey(prop.getName())) { 069 props.put(prop.getName(), entry.getValue() ? 070 config.getSubst().replace(prop.getValue()) : prop.getValue()); 071 } 072 } 073 074 return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getFQCN(), source.getLevel(), 075 source.getMessage(), source.getThrown(), props, source.getContextStack(), source.getThreadName(), 076 source.getSource(), source.getMillis()); 077 } 078 079 @Override 080 public String toString() { 081 StringBuilder sb = new StringBuilder(); 082 sb.append(" {"); 083 boolean first = true; 084 for (Map.Entry<Property, Boolean> entry : properties.entrySet()) { 085 if (!first) { 086 sb.append(", "); 087 } 088 Property prop = entry.getKey(); 089 sb.append(prop.getName()).append("=").append(prop.getValue()); 090 first = false; 091 } 092 sb.append("}"); 093 return sb.toString(); 094 } 095 096 /** 097 * The factory method to create the PropertiesRewritePolicy. 098 * @param config The Configuration. 099 * @param props key/value pairs for the new keys and values. 100 * @return The PropertiesRewritePolicy. 101 */ 102 @PluginFactory 103 public static PropertiesRewritePolicy createPolicy(@PluginConfiguration Configuration config, 104 @PluginElement("properties") Property[] props) { 105 if (props == null || props.length == 0) { 106 LOGGER.error("Properties must be specified for the PropertiesRewritePolicy"); 107 return null; 108 } 109 List<Property> properties = Arrays.asList(props); 110 return new PropertiesRewritePolicy(config, properties); 111 } 112 }