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.LogEvent; 026import org.apache.logging.log4j.core.config.Configuration; 027import org.apache.logging.log4j.core.config.Property; 028import org.apache.logging.log4j.core.config.plugins.Plugin; 029import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 030import org.apache.logging.log4j.core.config.plugins.PluginElement; 031import org.apache.logging.log4j.core.config.plugins.PluginFactory; 032import org.apache.logging.log4j.core.impl.Log4jLogEvent; 033import org.apache.logging.log4j.status.StatusLogger; 034 035/** 036 * This policy modifies events by replacing or possibly adding keys and values to the MapMessage. 037 */ 038@Plugin(name = "PropertiesRewritePolicy", category = "Core", elementType = "rewritePolicy", printObject = true) 039public 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(final Configuration config, final List<Property> props) { 050 this.config = config; 051 this.properties = new HashMap<Property, Boolean>(props.size()); 052 for (final Property property : props) { 053 final Boolean interpolate = Boolean.valueOf(property.getValue().contains("${")); 054 properties.put(property, 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 @Override 065 public LogEvent rewrite(final LogEvent source) { 066 final Map<String, String> props = new HashMap<String, String>(source.getContextMap()); 067 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) { 068 final Property prop = entry.getKey(); 069 props.put(prop.getName(), entry.getValue().booleanValue() ? 070 config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue()); 071 } 072 073 return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getLoggerFqcn(), source.getLevel(), 074 source.getMessage(), source.getThrown(), props, source.getContextStack(), source.getThreadName(), 075 source.getSource(), source.getTimeMillis()); 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}