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