package org.apache.logging.log4j.core.appender.rewrite;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.impl.ContextDataFactory;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.StringMap;
@Plugin(name = "PropertiesRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
public final class PropertiesRewritePolicy implements RewritePolicy {
protected static final Logger LOGGER = StatusLogger.getLogger();
private final Map<Property, Boolean> properties;
private final Configuration config;
private PropertiesRewritePolicy(final Configuration config, final List<Property> props) {
this.config = config;
this.properties = new HashMap<>(props.size());
for (final Property property : props) {
final Boolean interpolate = Boolean.valueOf(property.getValue().contains("${"));
properties.put(property, interpolate);
}
}
@Override
public LogEvent rewrite(final LogEvent source) {
final StringMap newContextData = ContextDataFactory.createContextData(source.getContextData());
for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
final Property prop = entry.getKey();
newContextData.putValue(prop.getName(), entry.getValue().booleanValue() ?
config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue());
}
return new Log4jLogEvent.Builder(source).setContextData(newContextData).build();
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(" {");
boolean first = true;
for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
if (!first) {
sb.append(", ");
}
final Property prop = entry.getKey();
sb.append(prop.getName()).append('=').append(prop.getValue());
first = false;
}
sb.append('}');
return sb.toString();
}
@PluginFactory
public static PropertiesRewritePolicy createPolicy(@PluginConfiguration final Configuration config,
@PluginElement("Properties") final Property[] props) {
if (props == null || props.length == 0) {
LOGGER.error("Properties must be specified for the PropertiesRewritePolicy");
return null;
}
final List<Property> properties = Arrays.asList(props);
return new PropertiesRewritePolicy(config, properties);
}
}