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